Jörg Reinholz: [PHP] Problem mit auflisten von Unterordnern

Beitrag lesen

Moin!

<?php
# Dein Code ist nicht gut aufgeräumt, ich habe das mal geändert.
$myfolder = '/var/www'; # Hier statisch, weil in der Konsole getestet.

# So wie Du das anfängst brauchst Du eine globale Variable für den Array:
$GLOBALS['folder_array']=array(); 
list_folder($myfolder);
asort($GLOBALS['folder_array']);
foreach($GLOBALS['folder_array'] AS $folder){
  echo $folder."<br>\n";
}
exit; # "Deklaratorisch", damit der Progger weiß, hier ist das Hauptprogramm zu Ende


function list_folder($path){
  if( $handle = @opendir($path) ) { 
  #Hinweis 
  #@ ist "böse", Du bekommst nicht mit, wenn ein Verzeichnis nicht lesbar ist
    while (false !== ($file = readdir($handle))){
      if ($file != "." && $file != ".." && is_dir($path."/".$file) ) {
        $GLOBALS['folder_array'][] = $path.'/'.$file;
        list_folder($path.'/'.$file); # das hier nennt sich "rekursiver Funktionsaufruf"
      }
    }
  closedir($handle);
  }
}
?>

Allerdings solltest Du noch links ausschließen:

…
if ($file != "." && $file != ".." && ! is_link($path."/".$file) && is_dir($path."/".$file) ) {
…

Jörg Reinholz