【问题标题】:PHP force download can't open files after downloadPHP强制下载下载后无法打开文件
【发布时间】:2013-12-07 09:34:45
【问题描述】:

我正在尝试实现 PHP 强制下载并使用下面的代码

$length = sprintf("%u", filesize($path));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($path).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $length);
set_time_limit(0);
readfile($path);
exit;

文件以完整大小成功下载(我在记事本++中打开了文件,没有 php 错误或警告),但我无法打开文件。奇怪的是我可以毫无问题地下载和打开 pdf 文件。但是文件格式如 docx、png、jpg 是合适的

【问题讨论】:

  • 这些反引号是干什么用的?
  • 当我在这里提出问题时以某种方式添加它。它不存在于真实代码中
  • 我在这里回答了类似的问题:stackoverflow.com/questions/23192683/…
  • 它帮助了我 :) @Seti

标签: php force-download


【解决方案1】:

在标题前添加ob_get_clean();ob_clean(); flush();readfile(); 之前

我遇到了同样的问题,这为我解决了。

【讨论】:

    【解决方案2】:
        $file=FCPATH."downloaded/1462026746_IMG_2015.JPG"; //file location 
            if(file_exists($file)) {
    
            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;
            }
            else {
                die('The provided file path is not valid.');
            }
    

    【讨论】:

    • 这可以正常工作。请刷新浏览器下载窗口。直到下载完成,请稍候。
    【解决方案3】:

    试试这个代码

    $file = "xyz.html"
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    readfile($file);
    

    【讨论】:

    • 我在 Notepad++ 中打开文件时没有显示错误。我将原始文件与下载的文件进行了比较。我发现的区别是原始文件有一些 unicode 字符,而下载的文件没有显示它(它显示了一些像“EFR TRS”这样的字母)。
    【解决方案4】:

    我认为你应该使用正确的内容类型

    以下是内容类型列表: http://www.freeformatter.com/mime-types-list.html

    【讨论】:

      【解决方案5】:

      用它来强制下载

      header("Cache-Control:  maxage=1");
      header("Pragma: public");
      header("Content-type: application/force-download");
      header("Content-Transfer-Encoding: Binary");
      header("Content-length: ".filesize($path));
      header("Content-disposition: attachment; filename=\"".basename($path)."\"");
      

      【讨论】:

        【解决方案6】:

        尝试暂时删除您的 content-length 标头,这可能是错误的并导致文件下载不完整(可能是单个字节)。还可以尝试将到期时间设置为高于 0;我在 IE 中看到了由此引起的错误,因为浏览器在外部程序有时间加载文件之前就从缓存中删除了该文件。

        【讨论】:

        • 感谢 SpliFF,这对我来说非常有效。我有同样的问题,接受 Content-Type: 设置为正确的内容类型(应用程序/pdf)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-20
        • 1970-01-01
        相关资源
        最近更新 更多