How can PHP be used to force the "Save As" dialog for file downloads in different browsers?
To force the "Save As" dialog for file downloads in different browsers using PHP, you can set the appropriate headers in the response. By setting the Content-Disposition header to "attachment", you can prompt the browser to show the "Save As" dialog instead of trying to display the file in the browser window.
<?php
$file = 'path_to_your_file.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
- In what ways can PHP developers optimize the user experience when implementing sorting functionality for SQL queries in PHP forums?
- How can one troubleshoot a "Forbidden" error message when accessing a file generated by PHP on a local Xampp server?
- How can the code in the provided PHP script be optimized to avoid SQL injection vulnerabilities?