How does the PHP script in the forum thread recursively scan directories and display them as links?

The PHP script can recursively scan directories using a function that iterates through each directory and subdirectory, displaying them as links. This can be achieved by using a recursive function that reads the contents of each directory, checks if it is a directory itself, and then calls itself to scan the subdirectories. Each directory found is displayed as a link.

<?php
function scanDirectories($dir) {
    $files = scandir($dir);
    
    foreach($files as $file) {
        if ($file != '.' && $file != '..') {
            if (is_dir($dir . '/' . $file)) {
                echo '<a href="' . $dir . '/' . $file . '">' . $file . '</a><br>';
                scanDirectories($dir . '/' . $file);
            }
        }
    }
}

// Call the function with the starting directory
scanDirectories('path/to/starting/directory');
?>