What is the best way to list the contents of a directory in PHP and display them as links on a webpage?

To list the contents of a directory in PHP and display them as links on a webpage, you can use the `scandir()` function to get an array of files in the directory, then loop through the array to create links for each file. You can use the `basename()` function to get just the filename without the full path.

$directory = "path/to/directory";
$files = scandir($directory);

foreach($files as $file){
    if($file != "." && $file != ".."){
        echo "<a href='$directory/$file'>$file</a><br>";
    }
}