<?php
function listFolder($dir, $indent = 0) {
    if (!is_readable($dir)) {
        echo str_repeat("   ", $indent) . "[Unreadable Directory]<br>";
        return;
    }

    $items = array_diff(scandir($dir), ['.', '..']);

    // Sort items alphabetically
    natcasesort($items);

    foreach ($items as $item) {
        $path = $dir . '/' . $item;

        if (is_dir($path)) {
            echo str_repeat("&nbsp;&nbsp;&nbsp;", $indent) . "|-- <strong>$item</strong> [DIR]<br>";
            listFolder($path, $indent + 1);
        } else {
            $size = filesize($path);
            $formattedSize = formatSize($size);

            echo str_repeat("&nbsp;&nbsp;&nbsp;", $indent) . "|-- $item ($formattedSize)<br>";
        }
    }
}

function formatSize($bytes) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $i = 0;
    while ($bytes >= 1024 && $i < count($units) - 1) {
        $bytes /= 1024;
        $i++;
    }
    return round($bytes, 2) . " " . $units[$i];
}

echo "<pre>";
listFolder(__DIR__);
echo "</pre>";
?>
