PHP:再次强制下载文件和 IE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1597732/
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
PHP: Force file download and IE, yet again
提问by Alex Weinstein
Folks, I know there have been lots of threads about forcing the download dialog to pop up, but none of the solutions worked for me yet.
伙计们,我知道有很多关于强制弹出下载对话框的线程,但没有一个解决方案对我有用。
My app sends mail to the user's email account, notifying them that "another user sent them a message". Those messages might have links to Excel files. When the user clicks on a link in their GMail/Yahoo Mail/Outlook to that Excel file, I want the File Save dialog to pop up.
我的应用程序向用户的电子邮件帐户发送邮件,通知他们“另一个用户向他们发送了一条消息”。这些消息可能包含指向 Excel 文件的链接。当用户单击 GMail/Yahoo Mail/Outlook 中指向该 Excel 文件的链接时,我希望弹出文件保存对话框。
Problem: when I right-click and do "Save As" on IE, i get a Save As dialog. When I just click the link (which many of my clients will do as they are not computer-savvy), I get an IE error message: "IE cannot download file ... from ...". May be relevant: on GMail where I'm testing this, every link is a "target=_blank" link (forced by Google).
问题:当我在 IE 上右键单击并执行“另存为”时,出现一个另存为对话框。当我单击链接时(我的许多客户不熟悉计算机,他们会这样做),我收到一条 IE 错误消息:“IE 无法下载文件......来自......”。可能是相关的:在我正在测试的 GMail 上,每个链接都是“target=_blank”链接(由 Google 强制)。
All other browsers work fine in all cases.
所有其他浏览器在所有情况下都可以正常工作。
Here are my headers (captured through Fiddler):
这是我的标题(通过 Fiddler 捕获):
HTTP/1.1 200 OK
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Content-Length: 15872
Via: **** // proxy server name
Expires: 0
Date: Tue, 20 Oct 2009 22:41:37 GMT
Content-Type: application/vnd.ms-excel
Server: Apache/2.2.11 (Unix) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_python/3.3.1 Python/2.5.2 SVN/1.4.6 mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.10.0
Cache-Control: private
Pragma: no-cache
Last-Modified: Tue, 20 Oct 2009 22:41:37 GMT
Content-Disposition: attachment; filename="myFile.xls"
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
I want IE's regular left-click behavior to work. Any ideas?
我希望 IE 的常规左键单击行为能够正常工作。有任何想法吗?
回答by Corey Ballou
This will check for versions of IE and set headers accordingly.
这将检查 IE 的版本并相应地设置标题。
// assume you have a full path to file stored in $filename
if (!is_file($filename)) {
die('The file appears to be invalid.');
}
$filepath = str_replace('\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));
// use this unless you want to find the mime type based on extension
$mime = array('application/octet-stream');
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');
// check for IE only headers
if (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) || (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false)) {
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
} else {
header('Pragma: no-cache');
}
$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);
回答by Buzogany Laszlo
Just use:
只需使用:
header('Content-Disposition: attachment');
That's all. (Facebook does the same.)
就这样。(Facebook 也这样做。)
回答by txyoji
If you're trying to get the file to download every time, change the content type to 'application/octet-stream'.
如果您每次都尝试下载文件,请将内容类型更改为“application/octet-stream”。
Try it without the pragma statement.
在没有 pragma 语句的情况下尝试一下。
回答by EricLaw
Your no-cache directives (Vary, Expires, Pragma) are causing the problem.
您的无缓存指令(Vary、Expires、Pragma)导致了问题。

