What are the potential pitfalls of hardcoding file names in PHP headers for downloads?

Hardcoding file names in PHP headers for downloads can lead to issues if the file name contains special characters or is not properly sanitized. To avoid potential pitfalls, it is recommended to dynamically set the file name based on user input or file metadata. This can help prevent errors and ensure a smoother user experience.

// Dynamic file name based on user input or file metadata
$file_name = "example.pdf"; // Example file name

// Set headers for file download
header("Content-Disposition: attachment; filename=" . $file_name);
header("Content-Type: application/pdf");
readfile($file_path); // Output the file content
exit();