文章浏览 复制本页面地址

PHP 递归显示文件夹内容

[code]
/*
* @date : 2014-7-22
* @desc : get all of the file name by the Dir.
* @pramrm : $path
*/
function getFileByDir($path){
//此处应该设置白名单,防止任意目录便利,暂时不体现
$path .= substr($path,-1)=='/'?'':'/';
if(empty($path)) return false;
$handel = dir($path);
$i=0;
while(false!==($filename = $handel->read())){
if(($filename=='.')||($filename=='..')){
continue;
}
if(is_dir($path.$filename)){
$temp = getFileByDir($path.$filename);
if(!empty($temp))
$file_arr[] = $temp;
}else{
$file_arr[] = $path.$filename;
}
echo $path.$filename.'<br/>';
}
$handel->close();
return count($file_arr)>1 ? $file_arr:false;
}
$dir = dirname(__FILE__);
$arr = getFileByDir($dir);
echo '<pre>';
//var_dump($arr);
[/code]

标签:
上一篇:
下一篇: