Is it possible to override browser settings for specific file types using PHP headers?
When serving files from a PHP script, you can use PHP headers to control how the browser handles those files. To override browser settings for specific file types, you can use the "Content-Disposition" header with the "attachment" value to force the browser to download the file instead of displaying it. This is useful for file types that the browser may try to display, such as PDFs or images.
<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
exit;
?>
Keywords
Related Questions
- What are the differences between accessing Twitter data using URLs like https://twitter.com/phpfriends and API URLs like http://api.twitter.com/1/users/show.json?screen_name= in PHP scripts?
- Are there any security considerations to keep in mind when implementing a feature that allows users to directly upload changes to a CVS project using PHP?
- What are some potential pitfalls when trying to extract large amounts of data from a MySQL database using PHP?