PHP:强制文件下载和IE,再次(PHP: Force file download and IE,

2019-08-22 10:51发布

伙计们,我知道已经有很多有关强制下载对话框弹出线程,但没有解决方案的又为我工作。

我的应用程序发送邮件到用户的电子邮件帐户,通知他们说:“其他用户发送他们的消息。” 这些消息可能有联系的Excel文件。 当用户点击了自己的Gmail /雅虎邮件/展望到Excel文件中的链接,我想要的文件保存对话框弹出。

问题:当我点击右键并做“另存为”的IE浏览器,我得到一个另存为对话框。 当我点击链接(其中,因为它们不熟悉电脑的我的许多客户都行),我得到一个IE错误信息:“IE无法下载文件......”。 可能是相关的:在Gmail在那里我测试这一点,每一个环节都是一个“TARGET = _blank”链接(通过谷歌被迫)。

所有其他浏览器在所有情况下正常工作。

这里是我的头文件(通过小提琴手捕获):

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

我想IE的定期左键点击行为的工作。 有任何想法吗?

Answer 1:

这将检查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);


Answer 2:

只需使用:

header('Content-Disposition: attachment');

就这样。 (脸谱不相同。)



Answer 3:

如果你想获得要下载的文件每次更改内容类型为“应用程序/八位字节流”。

尝试没有编译语句。



Answer 4:

你无缓存指令(各不相同,到期后,附注)是造成问题的原因。

见http://blogs.msdn.com/ieinternals/archive/2009/10/02/Internet-Explorer-cannot-download-over-HTTPS-when-no-cache.aspx



文章来源: PHP: Force file download and IE, yet again