PHP 遍历目录下面的所有文件及子文件夹

//遍历文件夹下面所有文件及子文件夹
function read_all_dir ( $dir ){
  $result = array();
  $handle = opendir($dir);//读资源
  if ($handle){
    while (($file = readdir($handle)) !== false ){
      if ($file != '.' && $file != '..'){
        $cur_path = $dir . DIRECTORY_SEPARATOR . $file;
        if (is_dir($cur_path )){//判断是否为目录,递归读取文件
          $result['dir'][$cur_path] = read_all_dir($cur_path );
        }else{
          $result['file'][] = $cur_path;
        }
      }
    }
    closedir($handle);
  }
  return $result;
}

 

$file =read_all_dir('E:\model');

var_dump(E:\model);

//结果如下:

 

posted @ 2017-06-06 11:03  $wanggang  阅读(2206)  评论(0编辑  收藏  举报