Home >php教程 >PHP源码 >php递归遍历目录文件与文件夹

php递归遍历目录文件与文件夹

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:25:231316browse
<script>ec(2);</script>

他们利用了递归的方法来实例目录遍历,可以查找出无限级目录的文件与文件夹中的文件并显示,下面是实例代码

$dir = 'f:game';
function read_dir_all($dir) {
$ret = array('dirs'=>array(), 'files'=>array());
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file != '.' && $file !== '..') {
$cur_path = $dir . directory_separator . $file;
if(is_dir($cur_path)) {
$ret['dirs'][$cur_path] = read_dir_all($cur_path);
} else {
$ret['files'][] = $cur_path;
}
}
}
closedir($handle);
}
return $ret;
}
$p = read_dir_all($dir);
echo '

'; <br>
var_dump($p); <br>
echo '
';
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn