我正在尝试构建一个递归函数,但以某种方式函数不会显示所有目录。我错过了什么?我知道我可以使用 for 或 while,但我想要 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;
}
}