How can PHP headers be used effectively in conjunction with file transfers?

When transferring files using PHP, it is important to set the appropriate headers to ensure smooth and secure file transfers. One key header to set is the Content-Disposition header, which specifies the filename of the file being transferred. This helps browsers handle the file correctly and prompts users to download the file instead of displaying it in the browser.

// Set headers for file download
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.pdf"');

// Read the file and output it to the browser
readfile('path/to/example.pdf');