What are some best practices for displaying file names in a list menu in PHP?

When displaying file names in a list menu in PHP, it is important to sanitize the file names to prevent any potential security vulnerabilities such as code injection. One way to do this is by using the htmlentities() function to encode the file names before displaying them. Additionally, you can limit the length of the file names displayed to ensure they fit within the menu layout and are not excessively long.

// Get list of file names (replace this with your own logic)
$files = scandir('path/to/directory');

// Display file names in a list menu
echo '<select>';
foreach ($files as $file) {
    // Sanitize file name
    $safeFileName = htmlentities($file);
    
    // Limit file name length
    $displayFileName = (strlen($safeFileName) > 20) ? substr($safeFileName, 0, 20) . '...' : $safeFileName;
    
    echo '<option value="' . $file . '">' . $displayFileName . '</option>';
}
echo '</select>';