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);
?>