How can PHP be used to correctly set the MIME type for downloading a file?
When downloading a file using PHP, it is important to set the correct MIME type to ensure that the browser interprets the file correctly. This can be done using the header() function in PHP to specify the Content-Type header with the appropriate MIME type for the file being downloaded.
// Set the MIME type for downloading a file
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);