What are the best practices for using the glob() function in PHP to read directories and generate HTML lists?

When using the glob() function in PHP to read directories and generate HTML lists, it is important to follow best practices to ensure security and efficiency. One common best practice is to use the glob() function with a specific pattern to filter the files you want to include in the list. Additionally, it is recommended to sanitize the file names before displaying them in the HTML list to prevent any potential security vulnerabilities.

<?php
$files = glob('path/to/directory/*.{jpg,jpeg,png,gif}', GLOB_BRACE);

echo '<ul>';
foreach ($files as $file) {
    $fileName = basename($file);
    echo '<li>' . htmlspecialchars($fileName) . '</li>';
}
echo '</ul>';
?>