1. The Problem:
You're attempting to enable users to download files from your server but the file is not prompting a "Save As" dialog.
2. The Cause:
Incorrect content type declaration in the header.
3. The Solution:
Ensure that the content type header is set to application/octet-stream for file downloads:
header('Content-Type: application/octet-stream');
4. Additional Tips:
5. Example Code:
$quoted = sprintf('"%s"', addcslashes(basename($file), '"\')); $size = filesize($file); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $quoted); header('Content-Transfer-Encoding: binary'); header('Connection: Keep-Alive'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . $size); readfile($file);
The above is the detailed content of How to Force File Downloads with PHP Using `header()`?. For more information, please follow other related articles on the PHP Chinese website!