if(is_dir($path)){
$arr=array();
$d=opendir($path);
while($content=readdir($d)){
if($content!='.'&&$content!='..'){
$newPath=$path.'/'.$content;
$arr[]=$newPath;
getDir($newPath);
}
}
closedir($d);
return $arr;
}else{
return false;
}
}
$re=getDir('../file');
var_dump($re);
此返回值爲第一層目錄的內容:
==================================================
正確方法一:
function getDir($path){
if(is_dir($path)){
static $arr=array();
$d=opendir($path);
while($content=readdir($d)){
if($content!='.'&&$content!='..'){
$newPath=$path.'/'.$content;
$arr[]=$newPath;
getDir($newPath);
}
}
closedir($d);
return $arr;
}else{
return false;
}
}
$re=getDir('../file');
var_dump($re);
此返回值爲目錄內全部內容:
正確方法二:
function getDir($path){
if(is_dir($path)){
$arr=array();
$d=opendir($path);
while($content=readdir($d)){
if($content!='.'&&$content!='..'){
$newPath=$path.'/'.$content;
$arr[]=$newPath;
$arr1=getDir($newPath);
if(is_array($arr1)){
$arr=array_merge($arr,$arr1);
}
}
}
closedir($d);
return $arr;
}else{
return false;
}
}
$re=getDir('../file');
var_dump($re);