How can PHP be used to dynamically list PDF files in a specific directory when a user clicks on a folder?

To dynamically list PDF files in a specific directory when a user clicks on a folder, you can use PHP to scan the directory for PDF files and generate links to them. This can be achieved by using the scandir() function to get a list of files in the directory, filtering out non-PDF files, and then displaying them as clickable links for the user to download or view.

<?php
$directory = "path/to/directory";

$files = scandir($directory);

foreach($files as $file) {
    if(pathinfo($file, PATHINFO_EXTENSION) == 'pdf') {
        echo '<a href="' . $directory . '/' . $file . '">' . $file . '</a><br>';
    }
}
?>