What best practices should be followed when setting headers for file downloads in PHP to ensure compatibility with different browsers?
When setting headers for file downloads in PHP, it is important to ensure compatibility with different browsers by setting the correct content type and attachment disposition. This involves setting the "Content-Type" header to the appropriate MIME type of the file being downloaded and the "Content-Disposition" header to "attachment; filename=filename.extension".
<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
exit;
?>
Related Questions
- How can one prevent HTML tags from being inserted into PHP form fields and causing unexpected behavior?
- How can the PHP script be modified to search and replace values from a CSV file using the "search and replace" file from PEAR?
- What role does PathInfo play in determining the behavior of PHP scripts accessed through URLs?