遍历PHP目录下的文件夹和文件

 1 <?php
 2 //遍历PHP目录下的文件夹和文件名
 3 function bianli_dir($path){
 4     //定义数组用于存放遍历结果
 5     $files = array();
 6     if(is_dir($path)){
 7         //判断是否是目录路径
 8         if($handle = opendir($path)){
 9             //打开文件目录
10             while (($file = readdir($handle)) !== false){
11                 //读取文件目录
12                 if($file !='.' && $file !='..'){
13                     //排除.和..
14                     if(is_dir($path.'/'.$file)){
15                         $files[$file] = bianli_dir($path.'/'.$file);
16                     }else{
17                         $files[] = $path.'/'.$file;
18                     }
19                 }
20             }
21             closedir($handle);
22         }
23     }
24     return $files;
25 }
26 
27 echo '<pre>';
28 print_r(bianli_dir("D:/server/apache/htdocs/upload"));

 

posted @ 2015-10-04 21:05  lesuso  阅读(158)  评论(0编辑  收藏  举报