How can the header function in PHP be used to address problems with file downloads in different browsers?
The header function in PHP can be used to address problems with file downloads in different browsers by setting the appropriate content type and content disposition headers. This ensures that the browser knows how to handle the file being downloaded, such as prompting the user to save the file or opening it in a specific application.
<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
?>
Related Questions
- What are common errors or issues that can arise when including PHP files in a website?
- How can a lack of knowledge about PHP sessions impact the decision-making process when considering a switch from using JavaScript for storing shopping cart data?
- What are the advantages of using output buffering in PHP to manipulate HTML content dynamically?