What potential issue could arise from listing files in a directory using PHP?

One potential issue that could arise from listing files in a directory using PHP is exposing sensitive information, such as configuration files or user data, to unauthorized users. To solve this issue, you can restrict the files that are displayed by checking the file extension or using an access control mechanism.

$directory = 'path/to/directory';

$allowedExtensions = ['jpg', 'png', 'pdf']; // Define allowed file extensions

$files = scandir($directory);

foreach ($files as $file) {
    $fileInfo = pathinfo($file);
    if (in_array($fileInfo['extension'], $allowedExtensions)) {
        echo $file . "<br>";
    }
}