What is the purpose of the header function in PHP and how does it affect file downloads?

The header function in PHP is used to send raw HTTP headers. When used in the context of file downloads, the header function can be used to set the content type of the file being downloaded, as well as specify the filename that will be presented to the user when downloading the file. This function is commonly used to force a file download rather than displaying it in the browser.

<?php
// Set the content type and specify the filename for the file download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.txt"');

// Output the file content
echo "This is the content of the file.";
?>