<?php
// Detecta o caminho atual relativo à raiz do site
$currentPath = trim(str_replace($_SERVER['DOCUMENT_ROOT'], '', dirname(__FILE__)), '/');
$baseUrl = $currentPath ? '/' . $currentPath . '/' : '/';

function tree($dir, $prefix = '', $relativePath = '') {
    global $baseUrl;
    
    $items = array_diff(scandir($dir), ['.', '..']);
    $items = array_values($items);
    
    foreach ($items as $i => $item) {
        $isLast = ($i === count($items) - 1);
        $branch = $isLast ? '└── ' : '├── ';
        $isDir = is_dir("$dir/$item");
        $icon = $isDir ? '📁' : '📄';
        
        // Link agora inclui o caminho base
        $link = $baseUrl . $relativePath . $item . ($isDir ? '/' : '');
        
        echo $prefix . $branch . $icon . " <a href='$link'>$item</a><br>";
        
        if ($isDir) {
            tree("$dir/$item", $prefix . ($isLast ? '    ' : '│   '), $relativePath . $item . '/');
        }
    }
}

echo '<pre>';
echo "📁 " . ($currentPath ?: '.') . "<br>";
tree('.', '', '');
echo '</pre>';
?>