alxkolm

print directory

Sep 30th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2.  
  3. print_files(__DIR__);
  4.  
  5. /**
  6.  * @param $dirname Путь до директории
  7.  * @param int $level Уровень вложености
  8.  */
  9. function print_files($dirname, $level = 0){
  10.     $dir = opendir($dirname);
  11.  
  12.     while (($item  = readdir($dir)) !== false) {
  13.         if ($item === '.' || $item === '..'){
  14.             // Пропускаем служебные директрии
  15.             continue;
  16.         }
  17.  
  18.         // отступ
  19.         $intend = $level > 0 ? str_repeat(' ', 4 * ($level - 1)) . '|   ' : '';
  20.  
  21.         echo  $intend. $item . PHP_EOL;
  22.  
  23.         if (is_dir($dirname . '/' . $item)) {
  24.             // Рекурсивно выводим все поддиректории
  25.             print_files($dirname . '/' . $item, $level + 1);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment