What potential issues can arise when using the header function in PHP to trigger file downloads, and how can they be mitigated?

Issue: One potential issue when using the header function in PHP to trigger file downloads is that the headers must be sent before any output is generated. To mitigate this issue, you can use output buffering to capture the output and send it only after setting the headers.

<?php
ob_start();

// Set the appropriate headers
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.";

// Flush the output buffer
ob_end_flush();
?>