How can PHP be used to set the Content-type for different file types when downloading?
When downloading files using PHP, it is important to set the correct Content-type header to ensure that the browser interprets the file correctly. This can be done using the header() function in PHP to set the Content-type based on the file type being downloaded. By setting the Content-type correctly, you can ensure that the browser knows how to handle the file being downloaded.
$file = 'path/to/file.pdf'; // Path to the file to be downloaded
$filename = 'downloaded_file.pdf'; // Name of the file that will be downloaded
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($file);
exit;