How can developers ensure cross-browser compatibility when handling file downloads in PHP?
Developers can ensure cross-browser compatibility when handling file downloads in PHP by setting appropriate headers in the response to indicate the file type and force the browser to download the file instead of displaying it. This can be achieved by using the header() function in PHP to set the Content-Type and Content-Disposition headers.
<?php
$file = 'example.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
Related Questions
- How can the PHP code be adjusted to ensure that the page navigation occurs before jumping to the anchor link?
- Is it recommended to use a database like MySQL for storing forum posts in PHP instead of text files?
- How can regular expressions (regex) be used in PHP to identify and extract code blocks from a string?