【问题标题】:PHP script to download file not working in IE用于下载文件的 PHP 脚本在 IE 中不起作用
【发布时间】:2010-11-16 04:15:25
【问题描述】:

我有一个脚本,它从 $_GET['key'] 获取密钥,在数据库中查找位置,并使用 readfile 和一些标题来提供下载以供使用。这适用于 Firefox 但不适用于 IE8,无法在另一个 IE 上进行测试。我在 IE 中收到以下错误:“Internet Explorer 无法从 www.example.com 下载 download.php”。好像它正在尝试下载 PHP 脚本。


$the_query = "SELECT * FROM `files` WHERE `user_id`=" . $_SESSION['user_id'] . " AND `key`='" . $key . "'";

$result = mysql_query($the_query);
$row = mysql_fetch_array($result);

$file = '/var/www/vhosts/www.example.com/httpsdocs/uploads/' . $row['id'] . '/' . $row['file'];

header("Content-type: application/octet-stream");
header("Content-length: ".filesize($file));
header('Content-Description: File Transfer');
header("Cache-control: private");
header('Content-Disposition: attachment; filename=' . rawurlencode(basename($file)));
readfile($file);

【问题讨论】:

  • 看看SQL注入和mysql_real_escape_string。这段代码很容易受到攻击。
  • 天哪,是的,请使用mysql_real_escape_string
  • 这个不太了解,不过可能跟$file中的文件扩展名有关
  • 如果您的意思是在 $key 变量上,那么是的,我已经在使用它,只是不在上面的代码中。

标签: php http-headers


【解决方案1】:

解决错误:“Internet Explorer 无法从 www.example.com 下载 download.php”, 将这些标头添加到您的脚本中:

header("Pragma: ");

header("Cache-Control: ");

该代码将从导致下载问题的标头中删除 Cache-Control。

上面的代码应该加在文件的顶部。

它对我们来说很好。

【讨论】:

  • 感谢一百万从来自火星的外星虫子手中拯救了我的生命......我不知道我能做什么,但你的这件盔甲帮助我杀死了这个非常非常大的虫子......虫子死了......耶!......谢谢:-)
  • 我们在这个问题上绕来绕去。从博客文章等中尝试了很多建议。这是修复它的建议!
【解决方案2】:

通过使用 php.net 中的第一个示例成功地完成了这项工作

http://us3.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, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit;

【讨论】:

    【解决方案3】:

    替换这个:
    header("Content-type: application/octet-stream");
    用这个:
    header("Content-Type: application/force-download");

    根据post,IE 通常不会监听您的标头,而是自行查找您发送的内容。

    【讨论】:

    • 我之前已经尝试过了,同样的错误很遗憾,谢谢。
    • application/octet-stream 是用于下载数据的官方 MIME 媒体类型(请参阅 RFC 2046)。
    【解决方案4】:

    将 Content-Disposition 标头设置为“附件”,如下所示(在 PHP 中):附件是脚本化的。

    header('Content-Disposition: attachment');
    

    并在 .htaccess 中添加以下内容,并添加您想要下载的任何扩展名,而不仅仅是 txt

    <FilesMatch "\.(txt|pdf|csv|xls|xlsx|xlam|xlsb|xlsm|msg|doc|docx|mpg|jpg|png)">
       Header set Content-Disposition attachment
    </FilesMatch>
    

    【讨论】:

      【解决方案5】:

      只是一个提示,如果有人(比如我)在使用安全的 https 请求直接将文件下载输入地址栏时遇到问题。有一个 IE 错误导致此下载失败:

      http://support.microsoft.com/kb/323308/en-us

      似乎只有解决方法是根据文章设置缓存头。

      【讨论】:

        【解决方案6】:

        永远不要替换这个: header("Content-type: application/octet-stream");

        有了这个: header("Content-Type: application/force-download");

        “application/octet-stream”是最通用的,适用于大多数浏览器。

        我尝试在我的一个测试中使用“application/zip”,因为我在技术上处理的是一个 ZIP 文件,但是 IE6.0 损坏了下载!其他一切都表现正常。但是,是的,必须切换回“应用程序/八位字节流”,因此任何试图检测文件扩展名并切换到特定于扩展名的其他内容类型的代码都是有风险的!最好对所有二进制文件使用“application/octet-stream”!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-05
          • 1970-01-01
          • 2023-03-09
          相关资源
          最近更新 更多