用于下载文件 php 的标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13513420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Headers used to download file php
提问by jhodgson4
Possible Duplicate:
php, file download
可能重复:
php,文件下载
I have files that are not in the web root that I need to make available for download. So I have a script that uses the below to download the file requested. The problem is, I everyfile downloaded is corrupt? The files are ok because if I use FTP to download, they open. Here are the headers passed:
我有一些不在 Web 根目录中的文件需要下载。所以我有一个使用下面的脚本来下载请求的文件。问题是,我下载的每个文件都损坏了?这些文件没问题,因为如果我使用 FTP 下载,它们会打开。以下是传递的标头:
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for i.e.
header("Content-Type: " . $download[0]['mime']);
header("Content-Disposition: attachment; filename=" .$download_file);
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
readfile($attachment_location);
回答by Almir Saraj?i?
Here is an example of headers used for that:
http://php.net/manual/en/function.readfile.php
这是用于此的标题示例:http:
//php.net/manual/en/function.readfile.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

