if(!function_exists('directory_map'))
{
/*
* 创建目标映射,就是历遍目录所有文件,并加到数组中,如果是文件夹,则历遍文件夹,并以文件夹为数组键名创建二维数组
* 读取指定的目录并构建它的数组表示,目录中包含的子文件夹也将被映射
* @param string $source_dir 源路径
* @param int $directory_depth 要遍历的目录深度,0=完全递归,1=当前目录,等等
* @param bool $hidden 是否要显示隐藏文件
* @return array
*/
function directory_map($source_dir,$directory_depth=0,$hidden=false)
{
if($fp=@opendir($source_dir))
{
$filedata = array();
$new_depth = $directory_depth-1;
$source_dir = rtrim($source_dir,DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
while(false !==($file=readdir($fp)))
{
//移除'.','..',和隐藏文件[可选]
if($file==='.' or $file==='..' or ($hidden===false && $file[0]==='.'))
{
continue;
}
//判断子文件是否是文件夹,如果是则在后面添加斜杠/
is_dir($source_dir.$file) && $file .= DIRECTORY_SEPARATOR;
//如果遍历深度小于1,或是新深度大于1,及子文件为目录,则递归循环
if(($directory_depth < 1 or $new_depth >0) && is_dir($source_dir.$file))
{
$filedata[$file]=directory_map($source_dir.$file,$new_depth,$hidden);
}
else
{
$filedata[]=$file;
}
}
closedir($fp);
return $filedata;
}
return false;
}
}