PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP:再次强制文件下载和IE大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
伙计们,我知道有很多关于强制下载对话框弹出的线程,但是没有一个解决方案对我有用.

我的应用程序将邮件发送到用户的电子邮件帐户,通知他们“另一个用户向他们发送了一条消息”.这些消息可能包含指向Excel文件链接.当用户点击其GMail / Yahoo Mail / Outlook中的链接到该Excel文件时,我希望弹出“文件保存”对话框.

问题:当我右键单击并在IE上执行“另存为”时,我会看到另存为对话框.当我只是点击链接(我的许多客户将会这样做,因为他们不是精通计算机),我收到一条IE错误消息:“IE无法下载文件……来自…”.可能是相关的:在我测试的GMail上,每个链接都是一个“target = _blank”链接(谷歌强制).

所有其他浏览器在所有情况下都能正常工

这是我的标题(通过@R_404_1567@r捕获):

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的常规左键单击行为能够正常工作.有任何想法吗?

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

大佬总结

以上是大佬教程为你收集整理的PHP:再次强制文件下载和IE全部内容,希望文章能够帮你解决PHP:再次强制文件下载和IE所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: