您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php遍历文件的5种方式

2025/6/30 14:09:29发布10次查看
在网上搜索了一下遍历文件夹的函数,资料很多,但都不算特别的全。所以,今天粗略的总结一下,使用php 几种常见的遍历文件的方式。
一:使用scandir 函数
1.1 函数封装
scandir : 是php 自带函数,返回当前目录下的所有文件和文件夹。注意:会有 ‘.’ 和’..’,分别代编当前目录和上层目录。
/** * 使用scandir 遍历目录 * * @param $path * @return array */function getdir($path){ //判断目录是否为空 if(!file_exists($path)) { return []; } $files = scandir($path); $fileitem = []; foreach($files as $v) { $newpath = $path .directory_separator . $v; if(is_dir($newpath) && $v != '.' && $v != '..') { $fileitem = array_merge($fileitem, getdir($newpath)); }else if(is_file($newpath)){ $fileitem[] = $newpath; } } return $fileitem;}
1.2程序调用:
echo('遍历目录开始');$path = realpath('./m1/');var_dump(getdir($path));echo('遍历目录结束');
1.3 显示结果
/usr/local/opt/php@5.6/bin/php 遍历目录开始array(9) { [0]=> string(48) “/users/niedewang/phpstormprojects/test/m1/1.html” [1]=> string(48) “/users/niedewang/phpstormprojects/test/m1/2.html” [2]=> string(50) “/users/niedewang/phpstormprojects/test/m1/demo.txt” [3]=> string(54) “/users/niedewang/phpstormprojects/test/m1/m2/m2_1.html” [4]=> string(54) “/users/niedewang/phpstormprojects/test/m1/m2/m2_2.html” [5]=> string(54) “/users/niedewang/phpstormprojects/test/m1/m2/m2_3.html” [6]=> string(57) “/users/niedewang/phpstormprojects/test/m1/m2/m3/m3_1.html” [7]=> string(56) “/users/niedewang/phpstormprojects/test/m1/m2/m3/m3_2.php” [8]=> string(56) “/users/niedewang/phpstormprojects/test/m1/m2/m3/m3_3.txt” } 遍历目录结束 process finished with exit code 0
1.4 注意事项
难度系数:简单
注意事项: 注意循环的时候,过滤 ‘.’ 和 ‘..’ ,否则可能会导致死循环
二:使用glob函数
2.1 函数封装
glob:是php 子系统自带函数,功能和scandir 类似,但比它更加强大灵活。
比如 :glob(‘*.txt’),将会列举出目录下的所有为站名为.txt 的文件。
/** * 使用glob 遍历 * @param $path */function getdir2($path){ //判断目录是否为空 if(!file_exists($path)) { return []; } $fileitem = []; //切换如当前目录 chdir($path); foreach(glob('*') as $v) { $newpath = $path . directory_separator . $v; if(is_dir($newpath)) { $fileitem = array_merge($fileitem,getdir2($newpath)); }else if(is_file($newpath)) { $fileitem[] = $newpath; } } return $fileitem;}
2.2 函数调用方式
echo('遍历目录开始');$path = realpath('./m1/');var_dump(getdir2($path));echo('遍历目录结束');
2.3 :显示结果,同1.3 一致,不再展示
2.4 注意事项
难易程度:非常容易,不需要过滤 . 和 ..
灵活程度:非常灵活
三:使用opendir 函数
3.1 函数封装
opendir : 返回一个目录句柄,可以使用readdir 读取这个目录,也可以通过closedir 关闭这个目录
/** * 使用opendir 函数递归 */function getdir3($path){ if(!file_exists($path)) { return []; } $handle = opendir($path); $fileitem = []; if($handle) { while(($file = readdir($handle)) !== false) { $newpath = $path . directory_separator . $file; if(is_dir($newpath) && $file != '.' && $file != '..') { $fileitem = array_merge($fileitem,getdir3($newpath)); }else if(is_file($newpath)) { $fileitem[] = $newpath; } } } @closedir($handle); return$fileitem;}
3.2 调用方式
echo('遍历目录开始');$path = realpath('./m1/');var_dump(getdir5($path));echo('遍历目录结束');
3.3 注意事项
需要过滤 ‘.’ 和 ”
四:使用dir 函数
4.1 和opendir 函数非常类似,它更加的面向对象。
/** * dir 函数的方式,对象 * * @param $path * @return array */function getdir4($path) { if(!file_exists($path)) { return []; } $handel = dir($path); $fileitem = []; if($handel) { while(($file = $handel->read()) !== false) { $newpath = $path . directory_separator . $file; if(is_dir($newpath) && $file != '.' && $file != '..') { $fileitem = array_merge($fileitem,getdir4($newpath)); }else if(is_file($newpath)) { $fileitem[] = $newpath; } } } $handel->close(); return $fileitem;}
4.4 注意事项
需要过滤 ‘.’ 和 ”
五:使用spl 库
使用spl 库中的对象,好处是不需要进行迭代,书写起来非常容易
/** * 使用 spl 库 * * @param $path */function getdir5($path){ $dir = new recursivedirectoryiterator($path); $fileitem = []; foreach(new recursiveiteratoriterator($dir) as $k=>$v) { $filename = $v->getbasename(); if($filename != '.' && $filename != '..') { $fileitem[] = $k; } } return $fileitem;}
5.2 调用方式:
echo('遍历目录开始');$path = realpath('./m1/');var_dump(getdir4($path));echo('遍历目录结束');
5.3 注意事项:
难以程度:容易。
- 类名有点长,但非常容易使用,毕竟不需要自己写迭代函数了。
- 不需要处理 . 和 ..  两个特殊目录
六:总结
如果目录数量不是很多,建议使用 scandir 方式,应为函数名容易记忆。
如果要求灵活性,比如只显示某一类型的文件等,使用glob 方式,
本身就有类似搜索的功能。
如果目录下文件较多,建议使用opendir 和dir 的方式。
因为scandir 和glob都是返回满足条件的所有文件,如果有几十万这样,返回素组非常庞大。
opendir循环可控性要强一些,比如可以返回前100个文件等。这个还需要论证
如果希望代码简介,使用spl 库的方式。代码量少
更多相关教程请访问 php编程从入门到精通全套视频教程
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product