frankx: Verzeichnisse auslesen

Beitrag lesen

Hellihello

Warum wird das nicht in eine ordentlich benannte Funktion (oder Methode) gepackt? Warum fliegt hier Code irgendwie in der Gegend herum?

Historisch bedingt (Testlaborstadium):

  
<?php  
// read recursively through directory using SPL-Iterators and Dom Functions  
class RecursiveDirectoryReader  
{  
   // directory to recursively iterate through  
   private $_baseDirPath = "";  
  
   // instance of RecursiveDirectoryIterator predefined SPL-class  
   private $_recursiveDirectoryIterator = NULL;  
  
   // instance of RecursiveIteratorIterator passing over the recursive DirectoryIterator  
   private $_recursiveIteratorIterator = NULL;  
  
   // instanciate RecursiveDirectoryIterator and RecursiveIteratorIterator  
   public function __construct($basePath = ".")  
   {  
      // check if passed param is existing directory path  
      if (!is_dir($basePath)) {  
         return false;  
      }  
  
      $this->_baseDirPath = $basePath;  
  
    // instance of RecursiveDirectoryIterator predefined SPL-class  
      $this->_recursiveDirectoryIterator = new RecursiveDirectoryIterator($basePath);  
  
      // instance of RecursiveIteratorIterator passing over the recursive DirectoryIterator  
      $this->_recursiveIteratorIterator = new RecursiveIteratorIterator($this->_recursiveDirectoryIterator, true);  
  
      return $this;  
   }  
  
   // return a nested unordered list using DOM-functions  
   public function getNestedList()  
   {  
      // return if no iterators set  
      if (NULL === $this->_recursiveDirectoryIterator ||  
         NULL === $this->_recursiveIteratorIterator) {  
         return false;  
      }  
  
      $dom = new DOMDocument ("1.0");  
      // You like it nice indentation? Me too!  
      $dom->formatOutput = true;  
  
      // Tipp: Bitte sei konsistent bei Deinen Bezeichnern. Warum nicht komplett camelCase?  
      $baseUl = $dom->createElement("ul");  
      $dom->appendChild($baseUl);  
  
      // iterate recursive through the instance of RecursiveIteratorIterator  
      // Durchlaufe den Verzeichnisbaum.  Die Iteratoren ermöglichen die Traversierung des gesamten Baumes  
      // ohne dass wir uns selbst um die Rekursion kümmern müssen. Da spielen rekursive Iteratoren ihre Stärke aus!  
      // $path seems to be a dummy and not needed to be able to use "foreach"  
      foreach ( $this->_recursiveIteratorIterator as $path ) {  
  
         // create list entry  
         $li = $dom->createElement("li");  
  
         // Ausgabetext, aktuelle Verschachtelungstiefe und Pfad.  
         $depthAndPathInfo = "depth: " . $this->_recursiveIteratorIterator->getDepth() .  
                                    " - name: " . htmlentities ($path);  
         $li->appendChild( $dom->createTextNode($depthAndPathInfo) );  
  
         // check if on top level, then append to master-ul-element  
         // Wir befinden uns im Startverzeichnis, hänge das Listenelement  
         // mit dem Pfad des aktuellen Elementes in die äußere Liste  
         if ( $this->_recursiveIteratorIterator->getDepth() == 0 ) {  
            $baseUl->appendChild($li);  
         } else {  
          // if not on top level, append to the parent ul-element  
    $nestedUl[$this->_recursiveIteratorIterator->getDepth()-1]->appendChild($li);  
         }  
  
         // if currtent iteration-element is a directory, create an nested ul-element  
         if ($this->_recursiveIteratorIterator->hasChildren()) {  
            $nestedUl[$this->_recursiveIteratorIterator->getDepth()] = $dom->createElement("ul");  
            $li->appendChild($nestedUl[$this->_recursiveIteratorIterator->getDepth()]);  
         }  
      }  
      return $dom->saveXML();  
   }  
}  
// test  
$myRecursiveDirReader = new RecursiveDirectoryReader(".");  
echo $myRecursiveDirReader->getNestedList();

Dank und Gruß,

frankx

--
tryin to multitain  - Globus = Planet != Welt