What potential pitfalls should be considered when working with file names in PHP and populating a select box?

When working with file names in PHP and populating a select box, one potential pitfall to consider is the presence of special characters or spaces in file names. To avoid issues with displaying or accessing these file names in a select box, it is recommended to sanitize the file names by removing any special characters or spaces.

// Get the list of files in a directory
$files = scandir('path/to/directory');

// Sanitize file names by removing special characters and spaces
$options = array();
foreach ($files as $file) {
    $cleanFileName = preg_replace('/[^\w\-\.]/', '', $file); // Remove special characters
    $cleanFileName = str_replace(' ', '_', $cleanFileName); // Replace spaces with underscores
    $options[$cleanFileName] = $file;
}

// Populate a select box with sanitized file names
echo '<select>';
foreach ($options as $key => $value) {
    echo '<option value="' . $key . '">' . $value . '</option>';
}
echo '</select>';