I can not figure out this algorithm:

<?php // Пример печати дерева каталогов файловой системы: // Функция распечатывает имена всех подкаталогов в текущем каталоге, // выполняя рекурсивный обход. Параметр $level задает текущую // глубину рекурсии. function printTree($level=1) { // Открываем каталог и выходим в случае ошибки. $d = @opendir("."); if (!$d) return; while (($e=readdir($d)) !== false) { // Игнорируем элементы .. и . if ($e=='.' || $e=='..') continue; // Нам нужны только подкаталоги. if (!@is_dir($e)) continue; // Печатаем пробелы, чтобы сместить вывод. for ($i=0; $i<$level; $i++) echo " "; // Выводим текущий элемент. echo "$e\n"; // Входим в текущий подкаталог и печатаем его if (!chdir($e)) continue; printTree($level+1); // Возвращаемся назад chdir(".."); // Отправляем данные в браузер, чтобы избежать видимости зависания // для больших распечаток. flush(); } closedir($d); } // Выводим остальной текст фиксированным шрифтом echo "<pre>"; echo "\n"; // Входим в корневой каталог и печатаем его chdir($_SERVER['DOCUMENT_ROOT'].'/md1/'); PrintTree(); echo "</pre>"; ?> 

Or rather, I need to force it to display the directory tree in the wrong way:

enter image description here

And so:

Cadillac / Allante / 4T60

Cadillac / Allante / 4T80E

Cadillac / ATS / 6L45E

All folders have 3 levels mark / model / transmission

    1 answer 1

     function printTree($level = -1) { $dir = '.'; $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); if ($level > -1) { $iterator->setMaxDepth($level); } foreach ($iterator as $path => $obj) { if ($obj->isDir()) { echo $path . '<br>'; } } } printTree();