How can PHP be used to recursively read directories and display their contents on a webpage?

To recursively read directories and display their contents on a webpage using PHP, you can use a function that iterates through the directories and subdirectories, then display the files or folders found. This can be achieved by using the opendir(), readdir(), and is_dir() functions in PHP.

<?php
function displayDirectoryContents($dir) {
    $handle = opendir($dir);

    echo "<ul>";
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            $path = $dir . "/" . $entry;
            if (is_dir($path)) {
                echo "<li><strong>$entry</strong></li>";
                displayDirectoryContents($path);
            } else {
                echo "<li>$entry</li>";
            }
        }
    }
    echo "</ul>";

    closedir($handle);
}

$directory = "path/to/your/directory";
displayDirectoryContents($directory);
?>