首页 > php文件操作 > php递归遍历目录文件与文件夹

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

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

<?php
$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 '<pre>';
var_dump($p);
echo '</pre>';
?>


文章链接:http://www.phprm.com/wenjian/fs4165.html

随便收藏,请保留本文地址!

标签:遍历目录 php递归

相关文章

发表留言