首页 > 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/code/38377.html

欢迎收藏∩_∩但请保留本文链接。

标签:opendir

相关文章

发表留言