What is the purpose of using the headers "Content-Type", "Content-Length", "Content-Disposition" and "Content-Description" in PHP for file downloads?
The purpose of using headers like "Content-Type", "Content-Length", "Content-Disposition", and "Content-Description" in PHP for file downloads is to provide important information about the file being downloaded. These headers help the browser understand how to handle the file, such as displaying it in the browser or prompting the user to download it. By setting these headers correctly, you can ensure a smooth and predictable file download experience for users.
<?php
$file = 'example.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
?>
Related Questions
- How can error messages be customized and displayed for form field validation in PHP?
- What are some common mistakes to avoid when designing PHP classes, such as inconsistent handling of null values in constructors and setters?
- What are the potential pitfalls of using octal notation for color values in PHP?