How can PHP be configured to download a specific file as an attachment instead of displaying it as PHP?
To configure PHP to download a specific file as an attachment instead of displaying it as PHP, you can use the header() function to set the appropriate content type and attachment header. This will force the browser to download the file instead of displaying it in the browser window.
<?php
$file = 'example.pdf';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file) . "\"");
readfile($file);
exit;
?>
Keywords
Related Questions
- What are some best practices for ensuring smooth communication and variable sharing between included scripts in PHP?
- What are the best practices for securely handling communication between a form script on one domain and a database on another domain?
- What are the potential pitfalls of not using the LIMIT parameter in a SQL query for pagination in PHP?