php文件夹遍历的几种方法
2018-03-01 11:34
302 查看
函数
function dirTree() { if(!is_dir($path)) return []; $files = []; $dir = opendir($path); while($file = readdir($dir)) { if($file == '.' || $file == '..') continue; $new_path = trim($path, '/').'/'.trim($file, '/'); $files[] = $new_path; if(is_dir($new_path)){ $files = array_merge($files, $this->ergodicDir2($new_path)); } } closedir($dir); return $files; }
Directory类
function dirTree() { if(!is_dir($path)) return []; $files = []; $dir_h = dir($path); while($file = $dir_h->read()){ if($file == '.' || $file == '..') continue; $new_path = trim($path, '/').'/'.trim($file, '/'); $files[] = $new_path; if(is_dir($new_path)){ $files = array_merge($files, $this->ergodicDir3($new_path)); } } $dir_h->close(); return $files; }
迭代器
function dirTree() { if(!is_dir($path)) return []; $files = []; $dir = new \DirectoryIterator($path); foreach ($dir as $key => $file){ if($file->getFilename() == '.' || $file->getFilename() == '..'){ continue; } $files[] = $file->getPathname(); if($file->isDir()){ $files = array_merge($files, $this->ergodicDir5($file->getPathname())); } } return $files; }
function dirTree() { $files = array(); /**@var \SplFileInfo $file*/ foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)) as $file) { $filename = $file->getFilename(); if($filename == '.' || $filename == '..') continue; $type = ($file->isDir()) ? '/' : ''; $files[] = $file->getPathname() . $type; } return array_unique($files); }
相关文章推荐
- PHP遍历文件夹(三种方法)
- PHP数组的几种遍历方法
- VC下遍历文件夹中的所有文件的几种方法
- PHP使用glob方法遍历文件夹下所有文件的实例
- PHP使用glob方法遍历文件夹下所有文件
- PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)
- php中遍历二维数组的几种方法详解
- PHP使用递归方法遍历文件夹
- php遍历目录与文件夹的多种方法详解
- php数组的几种遍历方法
- PHP遍历数组的几种方法
- php遍历所有文件及文件夹的方法深入解析
- PHP递归遍历文件夹去除注释并压缩php源代码的方法示例
- PHP创建多级文件夹的几种方法
- 探讨php中遍历二维数组的几种方法详解
- PHP使用glob函数遍历目录或文件夹的方法
- php遍历所有文件及文件夹的方法
- PHP遍历数组的几种方法
- PHP遍历数组的几种方法
- 遍历文件夹下文件名 几种方法