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

PHP glob函数遍历目录/文件夹方法介绍

2024/3/13 21:24:40发布37次查看
如果说到php遍历目录我们很多朋友会想到是opendir与readdir了,这样就可以遍历目录并显示文件了,但在php中有一个更简洁的遍历目录的函数glob估计很少有人知道此函数,不过我觉得比起opendir与readdir要简单多了.
php glob函数的使用:glob-寻找与模式匹配的文件路径.
例子,代码如下:
';}$filelist2 = glob('images/*');for ($i = 0; $i < count($filelist2); $i++) { echo $filelist2[$i] . '
';}$filelist3 = glob('*');for ($i = 0; $i close();}//开始运行echo 目录为粉红色
\n;tree(./nowamagic);/***********************第二种实现办法:用readdir()函数************************/function listdir($dir) { if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ((is_dir($dir . / . $file)) && $file != . && $file != ..) { echo 文件名:, $file,
; listdir($dir . / . $file . /); } else { if ($file != . && $file != ..) { echo $file .
; } } } closedir($dh); } }}//开始运行listdir(./nowamagic);
好了大家都看到子glob与opendir与readdir遍历输入的写法了, 从代码简洁上来说glob是完胜了opendir与readdir了, 在功能实现上达到的是相同的效果呀, 所以推荐使用glob函数遍历目录吧 .
文章地址:
转载随意^^请带上本文地址!
该用户其它信息

VIP推荐

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