Skip to content

Instantly share code, notes, and snippets.

@olegopro
Created December 19, 2023 19:54
Show Gist options
  • Save olegopro/550b17120f768a6930884d0b79c9805b to your computer and use it in GitHub Desktop.
Save olegopro/550b17120f768a6930884d0b79c9805b to your computer and use it in GitHub Desktop.
Скрипт на PHP предназначен для создания текстового представления структуры директорий файловой системы. Он просматривает указанную директорию и все её поддиректории, генерируя список файлов и папок в виде дерева. Вывод этого списка происходит в виде иерархического дерева с символами, указывающими на вложенность.
<?php
function drawTree($directory, $prefix = '')
{
$files = array_diff(scandir($directory), array('.', '..'));
$totalFiles = count($files);
$fileCount = 0;
$output = '';
foreach ($files as $file) {
$fileCount++;
$isLast = ($fileCount === $totalFiles);
$output .= $prefix . ($isLast ? '└── ' : '├── ') . $file . PHP_EOL;
if (is_dir($directory . DIRECTORY_SEPARATOR . $file)) {
$newPrefix = $prefix . ($isLast ? ' ' : '│ ');
$output .= drawTree($directory . DIRECTORY_SEPARATOR . $file, $newPrefix);
}
}
return $output;
}
// Путь в каталогу
$directoryPath = '/Volumes/SSD256/www/vk-bot/frontend-vue/src';
// Включение буферизации вывода
ob_start();
echo drawTree($directoryPath);
$treeOutput = ob_get_clean();
// Запись вывода в файл
file_put_contents('directory_tree.txt', $treeOutput);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment