Advertisement
lignite0

print dir structure tree - php

Feb 15th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2.  
  3. function tree($path, $level = 0, $structure = '', &$onlyOnce = false)
  4. {
  5.     $elements = scandir($path);
  6.     $last = end($elements);
  7.     reset($elements);      
  8.    
  9.     $onlyOnce = true;
  10.  
  11.     foreach ($elements as $element) {
  12.  
  13.         if ($element[0] == '.') {
  14.             continue;
  15.         }
  16.  
  17.         $isLast = $element == $last;
  18.         $currentStructure = $structure . ($isLast ? '`' : '|') . '-- ';
  19.  
  20.         echo $currentStructure.$element.PHP_EOL;
  21.  
  22.         $subpath = $path.DIRECTORY_SEPARATOR.$element;
  23.  
  24.         if (is_dir($subpath)) {
  25.             $sep = $isLast ? '    ' : '|   ';
  26.             tree($subpath, $level+1, $structure.$sep, $onlyOnce);
  27.         }
  28.  
  29.         if ($isLast && $onlyOnce) {
  30.             echo $structure.PHP_EOL;
  31.             $onlyOnce = false;
  32.         }
  33.     }
  34. }
  35.  
  36. tree('../..');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement