Are there any best practices for naming downloaded files in PHP applications?

When downloading files in PHP applications, it is important to ensure that the file names are safe and user-friendly. One common best practice is to sanitize the file name to remove any potentially harmful characters and provide a meaningful name for the downloaded file. This can help prevent security vulnerabilities and improve the user experience.

// Sanitize and create a user-friendly file name for downloaded files
$originalFileName = 'example_file.txt';
$cleanFileName = preg_replace("/[^a-zA-Z0-9\_\-\.]/", '', $originalFileName);
$cleanFileName = strtolower($cleanFileName); // Convert to lowercase for consistency

header('Content-Disposition: attachment; filename="' . $cleanFileName . '"');