【问题标题】:PHP: Force file download and IE, yet againPHP:再次强制文件下载和 IE
【发布时间】:2009-10-20 22:45:22
【问题描述】:

伙计们,我知道有很多关于强制弹出下载对话框的线程,但没有一个解决方案对我有用。

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

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

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

这是我的标题(通过 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

我希望 IE 的常规左键单击行为能够正常工作。有任何想法吗?

【问题讨论】:

    标签: php file internet-explorer download


    【解决方案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);
    

    【讨论】:

    • 非常感谢。救了我!我又恢复了理智:)
    • 后检查和预检查不会按照您的想法去做。你应该把它们拿出来。
    • 添加了对 IE 11 的支持并修复了代码中的错误。
    • header('Content-Type: '.$mime); 行实际上会发送“Content-Type: Array”
    【解决方案2】:

    只需使用:

    header('Content-Disposition: attachment');
    

    就是这样。 (Facebook 也是如此。)

    【讨论】:

      【解决方案3】:

      如果您尝试每次都下载文件,请将内容类型更改为“application/octet-stream”。

      尝试不使用 pragma 语句。

      【讨论】:

        【解决方案4】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-25
          • 1970-01-01
          • 1970-01-01
          • 2011-07-04
          相关资源
          最近更新 更多