-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir.func.php
178 lines (167 loc) · 3.19 KB
/
dir.func.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
//打开指定目录,读的是最外层的内容
function readDirectory($path) {
$handle = opendir ( $path );
while ( ($item = readdir ( $handle )) !== false ) {
//.和..这2个特殊目录
if ($item != "." && $item != "..") {
if (is_file ( $path . "/" . $item )) {
$arr ['file'] [] = $item;
}
if (is_dir ( $path . "/" . $item )) {
$arr ['dir'] [] = $item;
}
}
}
closedir ( $handle );
return $arr;
}
//$path="file";
//print_r(readDirectory($path));
/**
* 得到文件夹大小
* @param string $path
* @return int
*/
function dirSize($path){
$sum=0;
global $sum;
$handle=opendir($path);
while(($item=readdir($handle))!==false){
if($item!="."&&$item!=".."){
if(is_file($path."/".$item)){
$sum+=filesize($path."/".$item);
}
if(is_dir($path."/".$item)){
$func=__FUNCTION__;
$func($path."/".$item);
}
}
}
closedir($handle);
return $sum;
}
//$path="file";
//echo dirSize($path);
/**
* 创建文件夹
* @param string $filename
* @return string
*/
function createFolder($dirname){
//检测文件夹名称的合法性
if(checkFilename(basename($dirname))){
//当前目录下是否存在同名文件夹名称
if(!file_exists($dirname)){
if(mkdir($dirname,0777,true)){
$mes="文件夹创建成功";
}else{
$mes="文件夹创建失败";
}
}else{
$mes="存在相同文件夹名称";
}
}else{
$mes="非法文件夹名称";
}
return $mes;
}
/**
* 复制文件夹
* @param string $filename
* @return string
*/
function copyFolder($src,$dst){
//echo $src,"---",$dst."----";
if(!file_exists($dst)){
mkdir($dst,0777,true);
}
$handle=opendir($src);
while(($item=readdir($handle))!==false){
if($item!="."&&$item!=".."){
if(is_file($src."/".$item)){
copy($src."/".$item,$dst."/".$item);
}
if(is_dir($src."/".$item)){
$func=__FUNCTION__;
$func($src."/".$item,$dst."/".$item);
}
}
}
closedir($handle);
return "复制成功";
}
/**
* 重命名文件夹
* @param string $oldname
* @param string $newname
* @return string
*/
function renameFolder($oldname,$newname){
//检测文件夹名称的合法性
if(checkFilename(basename($newname))){
//检测当前目录下是否存在同名文件夹名称
if(!file_exists($newname)){
if(rename($oldname,$newname)){
$mes="重命名成功";
}else{
$mes="重命名失败";
}
}else{
$mes="存在同名文件夹";
}
}else{
$mes="非法文件夹名称";
}
return $mes;
}
/**
* 剪切文件夹
* @param string $src
* @param string $dst
* @return string
*/
function cutFolder($src,$dst){
//echo $src,"--",$dst;
if(file_exists($dst)){
if(is_dir($dst)){
if(!file_exists($dst."/".basename($src))){
if(rename($src,$dst."/".basename($src))){
$mes="剪切成功";
}else{
$mes="剪切失败";
}
}else{
$mes="存在同名文件夹";
}
}else{
$mes="不是一个文件夹";
}
}else{
$mes="目标文件夹不存在";
}
return $mes;
}
/**
* 删除文件夹
* @param string $path
* @return string
*/
function delFolder($path){
$handle=opendir($path);
while(($item=readdir($handle))!==false){
if($item!="."&&$item!=".."){
if(is_file($path."/".$item)){
unlink($path."/".$item);//删除文件夹
}
if(is_dir($path."/".$item)){
$func=__FUNCTION__;//递归操作
$func($path."/".$item);
}
}
}
closedir($handle);
rmdir($path);//删除空目录文件
return "文件夹删除成功";
}
?>