How can PHP headers be utilized to control file downloads and prevent path exposure?

To control file downloads and prevent path exposure in PHP, you can use headers to set the content type and disposition of the file being downloaded. This ensures that the file is treated as an attachment and not displayed in the browser, preventing the exposure of the file path.

<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
exit;
?>