What is the purpose of the header("Content-Disposition: attachment; filename=".$dateiname) function in PHP?
The header("Content-Disposition: attachment; filename=".$dateiname) function in PHP is used to force the browser to download a file instead of displaying it in the browser. This is particularly useful when you want the user to download a file, such as a PDF or a CSV file, instead of viewing it directly in the browser. By setting the Content-Disposition header to "attachment" and providing a filename, the browser will prompt the user to save the file with the specified filename.
<?php
$dateiname = "example.pdf";
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=".$dateiname);
// Output the file content here
readfile($dateiname);
?>
Keywords
Related Questions
- How can Plesk be updated to a newer version to support PHP 5.3+?
- How can the simplification of PHP code, such as using empty() instead of isset() and optimizing variable assignments, improve the security and efficiency of form handling processes?
- How can PHP be used to update values in a database while limiting the addition to a maximum value?