Is there a way to manipulate the Content-Type header in PHP to influence how browsers handle the download of specific file types, such as PDFs?
To manipulate the Content-Type header in PHP to influence how browsers handle the download of specific file types, such as PDFs, you can set the Content-Type header to 'application/octet-stream' for PDF files. This forces the browser to treat the file as a download rather than attempting to display it in the browser window.
<?php
$file = 'example.pdf';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
?>
Keywords
Related Questions
- In what situations might removing a specific field from an update query in PHP result in different outcomes?
- How can one securely store and retrieve user information in PHP sessions?
- In what situations would it be more practical to normalize database tables rather than dynamically adding and removing fields in PHP scripts?