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;
?>
Related Questions
- What considerations should be taken into account when determining the relationship between Controllers and Views in PHP to ensure flexibility in changing Views without affecting the Controller logic?
- How can errors in parsing XML data be identified and resolved when attempting to display it in HTML format using PHP?
- How can the use of __DIR__ constant help in specifying file paths for includes in PHP?