【问题标题】:php - Filename of file when using firefox to downloadphp - 使用 firefox 下载时的文件名
【发布时间】:2013-08-08 09:53:48
【问题描述】:

我看到this 制作下载文件的教程,但我有一个问题
这是我的例子

$file_url = "D:/my file name.doc"

header('Content-Type: text/json; charset=UTF-8;');  
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file_url)."");
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_url));
ob_clean();
flush();
readfile($file_url);

在 ie 或 chrome 中一切正常。但是当我使用firefox 下载文件时。文件下载有my是文件名吗?怎么解决谢谢

【问题讨论】:

  • 可能是因为空格。我不认为你可以改变浏览器解释文件名的方式(不完全正确,因为 FF 是开源的)。在basename 上使用str_replace 将空格更改为例如下划线。
  • 您没有关闭 content-disposition 标头中的引用。试试这个: header('Content-Disposition: attachment; filename=' . basename($file_url)."'");

标签: php download


【解决方案1】:
header('Content-Disposition: attachment; filename="' .basename($file_url).'"');

【讨论】:

    【解决方案2】:

    首先,您在第一行末尾缺少;。 我认为问题出在带空格的文件名中。 尝试使用readfile(urlencode($file_url)); 而不是readfile($file_url);

    【讨论】:

      【解决方案3】:

      你应该引用文件名。

      header('Content-Disposition: attachment; filename="' . basename($file_url) . '"');
      

      编辑:这是选定的答案,只是其中有错字。现在我修好了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        • 2012-02-13
        • 1970-01-01
        • 1970-01-01
        • 2015-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多