Bacysoft.cn
标题:
PHP 文件夹遍历
[打印本页]
作者:
bacy001
时间:
2013-3-27 11:03
标题:
PHP 文件夹遍历
在开发PHP应用的过程中,经常会碰到需要遍历某个指定的文件夹,包括这个文件夹下的所有文件和子文件夹!
这里分享一个用 PHP 写的实现文件夹遍历功能的函数,具体代码如下:
function traversal($dir){
$arr = array();
$flist = glob($dir);
foreach($flist as $fn){
if(filetype($fn)=='dir'){
$arr[] = traversal($fn."/*");
}else{
$arr[] = $fn;
}
}
return $arr;
}
复制代码
该函数只实现简单遍历功能,返回值为指定目录的目录结构并保存在数组中,输出如下:
Array
(
[0] => Array
(
[0] => Array
(
[0] => develop/image/Thumbs.db
[1] => develop/image/bg_arrow_up.png
[2] => develop/image/bg_dev_hd.png
[3] => develop/image/bg_dev_v2.png
)
[1] => Array
(
[0] => develop/include/export.php
[1] => develop/include/hooklist.php
[2] => develop/include/language.php
[3] => develop/include/list.php
)
[2] => develop/plugin.lang.php
[3] => develop/plugin.php
[4] => Array
(
[0] => develop/template/ajax.htm
[1] => Array
(
[0] => develop/template/common/developer.css
[1] => develop/template/common/footer.htm
[2] => develop/template/common/header.htm
[3] => develop/template/common/showmessage.htm
)
[2] => develop/template/common.js
[3] => develop/template/language.htm
)
)
)
复制代码
如需更复杂的功能,比如:遍历的同时对文件或者子文件夹做对应的操作,只需要在本函数基础上,添加一个处理函数即可。
欢迎光临 Bacysoft.cn (http://bacysoft.cn/)
Powered by Discuz! X3.3