php - RecursiveDirectoryIterator indent subfolders -


i list files , folders using recursivedirectoryiterator, , display result same way explorer do. need indent subfolders, have no idea how that... use subfolder counter, work level 1, level 2 wouldn't work...

here's code :

    $nb=1;     $read_folder = './cases/';     $iterator = new recursiveiteratoriterator(new recursivedirectoryiterator($read_folder), recursiveiteratoriterator::self_first);     foreach ($iterator $file) {     $nb++;     if ($file->isdir()) {         $list_docs .= '<li class="docresult"><i class="fa fa-folder"></i>&nbsp;<span id="file_'.$nb.'">'.$file->getfilename().'</span></li>';        } else {         $list_docs .= '<li class="docresult"><i class="fa fa-file-o"></i>&nbsp;<span id="file_'.$nb.'">'.$file->getfilename().'</span></li>';        } } 

this version 2 of program traverse directory tree.

it has different program because of requirement sort output directories , printed before files.

now, seemed simple when started searching answers. hmm... ;-/

i came conclusion either have use ready made package such synfony : finder component or write own directory iterator component.

i have done latter , made own sorteddirectory class.

the output not formatted in way. uses default styling of browser.

the source code class sorteddirectory available.

the code:

<?php // http://stackoverflow.com/questions/31470421/recursivedirectoryiterator-indent-subfolders  include 'q31470421-sorted-directory-class.php';  $sourcefolder = 'p:\developer\xampp\htdocs\testmysql\q23408272'; // top level directory search // $sourcefolder = 'p:\developer\xampp\htdocs\testmysql'; // top level directory search  /**  * sorted directory supplied path  */ $diriter = new sorteddirectory(new splfileinfo($sourcefolder));  /**  * process complete path recursing around required..  */ echo processdirectory($diriter);  exit; // end program  /**  * recurse around paths  *  * @param \sorteddirectory $directory  */ function processdirectory(\sorteddirectory $directory, $depth = 0) {     $html = showonedirectory($directory, $depth); // show files in directory      foreach ($directory $entry) {         if ($entry->isdir()) { // recurse arount sub-directories             $html .= '<ul class="dir-start">';             $html .=  processdirectory(new \sorteddirectory($entry), $depth + 1);             $html .= '<!-- dir-end --></ul>';         }     }      return $html; }  /**  * process files 1 directory  *  * @param \sorteddirectory $directory  * @param type $depth  * @return html  */ function showonedirectory(\sorteddirectory $directory, $depth = 0) {     $dircount = 0;     $filecount = 0;     $list_docs = '<ul>';     foreach ($directory $key => $entry) {           if ($entry->isdir()) {             $dircount++;             $list_docs .= '<li class="docresult">'                           .'<i class="fa fa-folder">'                           .'</i>&nbsp;<span id="file_'. $key .'">'                           .' directory: '                           .' name: '. $entry->getfilename()                           .' | path: '. $entry->getpath()                           .' | depth: '. $depth                          .'</span></li>';         }         else {             $filecount++;             $list_docs .= '<li class="docresult">'                           .'<i class="fa fa-folder">'                           .'</i>&nbsp;<span id="file_'. $key .'">'                           .' file: '                           .' name: '. $entry->getfilename()                           .' | path: '. $entry->getpath()                           .' | depth: '. $depth                          .'</span></li>';         }     }     $list_docs .= '<li>'. "counts: dir: $dircount, files: $filecount" .'</li>';     return $list_docs .= '</ul>'; } 

the sorteddirectory class:

if want change sort order edit:

private function cmpentry(splfileinfo $entry1, splfileinfo $entry2)

<?php // http://stackoverflow.com/questions/31470421/recursivedirectoryiterator-indent-subfolders       // q31470421-sorted-directory-class  class sorteddirectory implements iteratoraggregate, countable {     /**      * sorted directory list      *      * @var array      */     private $directories = array();      /**      * sorted file list      *      * @var array      */     private $files = array();      private $allfiles = null;      /**      * read supplied directory 2 lists , sort them      *      * ignore '.' , '..' entries      *      * @param splfileinfo      */     public function __construct(\splfileinfo $directory)     {        foreach (new directoryiterator($directory->getrealpath()) $entry) {            if ($entry->isdir()) {                $fname = $entry->getbasename();                if (substr($fname, 0, 1) === '.') {                    if ($fname === '.' || $fname === '..') {                        continue;                    }                 }                 $this->directories[] = clone $entry;            }            else {                $this->files[] = clone $entry;            }        }        $this->sortlists();     }       /**      * iterator of sorted directory list      *      * @return \arrayiterator      */     public function getdiriterator()     {        return new arrayiterator($this->directories);     }      /**      * iterator of sorted files      *      * @return \arrayiterator      */     public function getfileiterator()     {        return new arrayiterator($this->files);     }      /**      * iterator of directory entries      *      * @return \arrayiterator files directories before files      */     public function getiterator()     {        return new arrayiterator($this->allfiles);     }      public function __get($property)     {        return $this->$property;     }      /*      * implement iterator      *      */      /**      * @return \splfileinfo      */     public function current()     {         return current($this->allfiles);     }      /**      * @return int      */     public function key()     {         return key($this->allfiles);     }      public function next()     {         return next($this->allfiles);     }      public function reset()     {         return reset($this->allfiles);     }      public function rewind()     {         return reset($this->allfiles);     }      public function valid()     {         return current($this->allfiles) !== false;     }      /**      * @return integer      */     public function count()     {         return count($this->allfiles);     }      /**      * sort lists using 'natural' compare      */     private function sortlists()     {        if (count($this->directories) >= 2) {             usort($this->directories, array($this, 'cmpentry'));        }        if (count($this->files) >= 2) {             usort($this->files,  array($this, 'cmpentry'));        }         $this->allfiles = array_merge($this->directories, $this->files);     }      private function cmpentry(splfileinfo $entry1, splfileinfo $entry2)     {        return strnatcasecmp($entry1->getfilename(), $entry2->getfilename());     }  } 

Comments