php遍历文件夹下的文件夹及文件

时间:2014-10-10 12:01:54   收藏:0   阅读:174
function traverse($path = ‘.‘) {
   $current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
   while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
       $sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
       if($file == ‘.‘ || $file == ‘..‘) {
           continue;
       } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
           echo ‘Directory ‘ . $file . ‘:<br>‘;
           traverse($sub_dir);
       } else {    //如果是文件,直接输出
           echo ‘File in Directory ‘ . $path . ‘: ‘ . $file . ‘<br>‘;
       }
   }
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!