我想构建一个递归函数,但在某种方式函数不会显示所有的目录。我缺少什么?我知道我可以使用或一段时间,但我想foreach循环。下面是代码:用foreach循环递归
$GLOBALS['testmodus']=0;
$o=new BrowseFolder();
$o->browse('../images/',1);
ta($o);
function ta($ausgabe) {
echo('<p class="ta">'.__LINE__.$ausgabe.'</p>');
}
class BrowseFolder{
private $srcFolder;
private $ifRecursiv=0;
private $excludeFileTypes=array('.','..');
private $filesFound;
private $deleteFolderPath=0;
private $makeFolderPath;
public function browse($srcFolder,$ifRecursiv=0){
ta($this->filesFound);
$this->srcFolder=$srcFolder;
$this->ifRecursiv=$ifRecursiv;
$this->filesFound=scandir($this->srcFolder);
$this->filesFound=$this->excludeArrayFromArray($this->filesFound,$this->excludeFileTypes);
echo "<ul>";
foreach($this->filesFound as $val){
if(is_file($this->srcFolder.$val)){
echo "<li>";
echo "$val";
echo "</li>";
}elseif(is_dir($this->srcFolder.$val) && $this->ifRecursiv==1){
ta($this->srcFolder.$val);
echo "$val";
$this->browse($this->srcFolder.$val.'/',$this->ifRecursiv);
}
}
echo "</ul>";
}
private function excludeArrayFromArray($baseArray,$arrayToExclude){ //Exclude an array from another array,and arrange
$newArray=array_values(array_diff($baseArray,$arrayToExclude));
return $newArray;
}
}
您可以添加更多关于它不通过所有文件夹的方式吗?你有没有考虑过使用DirectoryIterator? http://php.net/manual/en/class.directoryiterator.php –
@ Ronni Skansing - 可能需要递归版本:http://php.net/manual/en/class.recursivedireiteiterator。php – ChicagoRedSox
@ChicagoRedSox +1;) –