What is the purpose of the PHP function header() in the context of file downloads?
The purpose of the PHP function header() in the context of file downloads is to send HTTP headers to the browser that specify how the file should be handled. This includes setting the content type of the file (e.g. application/pdf for a PDF file) and prompting the browser to download the file instead of displaying it in the browser window.
<?php
$file_path = 'path/to/your/file.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
header('Content-Length: ' . filesize($file_path));
readfile($file_path);
exit;
?>