What are the potential pitfalls of saving file names in PHP when working with different browsers?
When saving file names in PHP, one potential pitfall is that different browsers may handle file names differently, leading to inconsistencies or errors. To solve this issue, it is recommended to sanitize file names by removing special characters and spaces, and ensuring that the file name is unique. This can help ensure compatibility across different browsers and prevent any potential issues.
// Sanitize file name by removing special characters and spaces
$originalFileName = $_FILES['file']['name'];
$cleanFileName = preg_replace("/[^a-zA-Z0-9.]/", "", $originalFileName);
// Ensure file name is unique
$uniqueFileName = time() . '_' . $cleanFileName;
// Save file with the sanitized and unique file name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $uniqueFileName);