What are best practices for adjusting HTTP headers in PHP to ensure proper file downloading functionality?
When downloading files in PHP, it is important to set the appropriate HTTP headers to ensure proper functionality. This includes setting the Content-Type header to specify the type of file being downloaded, Content-Disposition header to prompt the browser to download the file instead of displaying it, and Content-Length header to indicate the size of the file. Failure to set these headers correctly can result in unexpected behavior or errors when downloading files.
<?php
$file = 'example.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
Related Questions
- What are some potential pitfalls to be aware of when using the word_wrap() function in PHP for text formatting?
- What are the potential issues with session configuration on servers when using PHP?
- What are the potential issues when using the include function in PHP to include files from different directories?