How can PHP code be modified to always prompt the user to save a file for download, rather than opening it in the browser?
To always prompt the user to save a file for download rather than opening it in the browser, you can set the appropriate headers in the PHP code. This can be achieved by setting the Content-Disposition header to "attachment" along with a filename. This way, the browser will always prompt the user to save the file instead of displaying it.
<?php
// Set the appropriate headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.txt"');
// Output the file content
echo "This is an example file content.";
?>
Keywords
Related Questions
- What are the best practices for updating values in a database using PHP to ensure data integrity?
- What alternative methods or functions could be used to export a single image to a CSV file in PHP?
- What are some key differences between the PHP code examples provided in the forum thread, and how do they impact the occurrence of the error message "Notice: Undefined offset"?