What potential issues can arise when setting headers for file downloads in PHP?
One potential issue that can arise when setting headers for file downloads in PHP is that the headers must be set before any output is sent to the browser. If any output is sent before setting the headers, it can cause errors or unexpected behavior. To solve this issue, make sure to set the headers at the beginning of the script before any other code.
<?php
// Set headers for file download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.txt"');
// Output file content
echo "This is the content of the file.";
?>