1 <?php
2 /*遍历目录,列出目录中的文件
3 * array scandir(string $directory [,int $sorting_order])
4 * $directory为待遍历目录的路径名,$sorting_order为可选参数,设置文件的降序或者升序排列
5 * */
6 $path='./'; //为当前目录
7 if(file_exists($path)){
8 $files_asc=scandir($path);
9 $files_desc=scandir($path,1);
10
11 echo '<p>该目录下的文件(升序排列):<br>';
12 print_r($files_asc);
13 echo '<p>该目录下的文件(降序排序):<br>';
14 print_r($files_desc);
15 }
16 else{
17 echo'该目录不存在!';
18 }
19
20 /*递归的遍历所在目录及其所有子目录,即所谓的遍历目录树*/
21 /*
22 * 递归函数
23 * 遍历目录树
24 * 输入参数:目录路径
25 * 输出结果:多维数组表示的目录树
26 * */
27 function GetDirFree($path){
28 $tree = array();
29 $tmp = array();
30
31 if(!is_dir($path)) return null; //如果不是路径则返回null
32
33 $files = scandir($path); //列出当前目录下的所有文件和目录
34
35 foreach($files as $value){
36 if($value=='.'||$value=='..') //跳过当前的目录名和父目录名
37 continue;
38
39 $full_path = $path.'/'.$value; //获取子文件或目录的完整路径
40 if(is_dir($full_path)){
41 $tree[$value]=GetDirFree($full_path);
42 }
43 else{
44 $tmp[]=$value;
45 }
46 }
47 //将文件添加到结果数组末尾
48 $tree = array_merge($tree,$tmp);
49 return $tree;
50 }
51 //$path='./';
52 echo '<br>'.'递归遍历目录及其子目录';
53 print_r(GetDirFree($path));
54
55 /*复制、移动目录*/
56 /*递归函数
57 * 复制目录
58 * 输入参数:源目录路径,目的目录路径
59 * 输出目录;复制成功则返回TRUE,否则返回false*/
60
61 function copyDir($source_path,$dest_path){
62 if(!is_dir($source_path)){ //如果不是路径则返回false
63 return false;
64 }
65 if(!file_exists($dest_path)){ //如果不存在目录则创建目录
66 if(!mkdir($dest_path)) return false;
67 }
68
69 $files=scandir($source_path);
70 foreach($files as $value){
71 if($value=='.'||$value=='..') continue; //跳过当前的目录名和父目录名
72
73 $child_source_path=$source_path.'/'.$value; //获取子文件或目录的完整路径
74
75 $child_dest_path=$dest_path.'/'.$value;
76
77 if(is_dir($child_source_path)){ //如果存在子目录,则复制子目录
78 if(!copyDir($source_path, $dest_path)){
79 return false;
80 }
81 }
82 else {
83 if(!copy($child_source_path,$child_dest_path)){
84 return false; //复制子文件
85 }
86 }
87 }
88
89 return true;
90 }
91
92 $source_path='./test_dir';
93 $dest_path='./copy_test_dir';
94 $result=copyDir($source_path, $dest_path);
95 if($result) echo '目录复制成功';
96 else echo '目录复制失败';
97
98 /*递归删除目录
99 * 删除目录内容
100 * 输入参数:目录路径
101 * 输出结果:删除成功则返回true,否则返回false
102 * */
103 function delDir($path){
104 if(!is_dir($path)) return false;
105 if(!file_exists($path)) return false;
106
107 $files=scandir($path);
108 foreach($files as $value){
109 if($value=='.'||$value=='..') continue;
110
111 $child_path=$path.'/'.$value;
112 if(is_dir($child_path)){
113 if(!delDir($child_path)){
114 return false;
115 }
116 }
117 else{
118 if(!unlink($child_path)){
119 return false;
120 }
121 }
122 }
123 if(!rmdir($path)) return false;
124 return true;
125 }
126 $path='./copy_test_dir';
127 $result=delDir($path);
128 if($result) echo'目录删除成功';
129 else echo'目录删除失败';
130
131 /*移动目录,是复制目录和删除目录的结合
132 * 递归函数
133 * 移动目录内容
134 * 输入参数:原目录路径,目的目录路径
135 * 输出结果:移动成功则返回true,反正返回false
136 * 使用copydir()函数和deldir()函数*/
137 function moveDir($source_path,$dest_path){
138 if(!copyDir($source_path, $dest_path)) return false;
139 if(!delDir($source_path)) return false;
140 return true;
141 }
142
143 $source_path='./test_dir';
144 $dest_path='./move_test_dir';
145 $result=moveDir($source_path, $dest_path);
146 if($result) echo '目录移动成功!';
147 else echo'移动失败';
148
149 ?>
150