What potential issues can arise when trying to use headers like Content-Type and Content-Disposition for file downloads in PHP?

One potential issue that can arise when using headers like Content-Type and Content-Disposition for file downloads in PHP is that the headers must be set before any output is sent to the browser. If there is any output before setting these headers, it can result in errors or unexpected behavior. To solve this, make sure to set the headers at the beginning of your PHP script before any other output.

<?php
// Set the headers before any output
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="example.pdf"');

// Output the file content
readfile('path/to/example.pdf');
?>