How can headers in PHP be used to control file downloads?
To control file downloads using headers in PHP, you can set the appropriate headers to indicate that the response is a file download. This includes setting the Content-Disposition header with a value of "attachment" and specifying the filename. Additionally, you can set the Content-Type header to the appropriate MIME type for the file being downloaded.
<?php
$file = 'example.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;
?>