【问题标题】:php header file transfer corruptedphp头文件传输损坏
【发布时间】:2015-09-03 12:39:37
【问题描述】:

我的文件夹中有一些文件。我使用以下 php 代码将文件传输到浏览器(带有标题)。

我以 .7z 格式下载了正确长度的文件,但我无法解压缩。 如果我用 ftp 传输相同的文件,我可以毫无问题地解压缩它。 从我的服务器我可以毫无问题地解压缩它。所以错误在php中的某个地方

    private function pushToBrowser($file){
    if(!$file){ // file does not exist
        die('file not found');
    } else {
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        header("Content-length: ".filesize($file).";\n");

        // read the file from disk
        readfile($file);
    }
}

代码的用法

$this->pushToBrowser($path);

【问题讨论】:

  • 我猜你发送了错误的内容类型?尝试将 content-type 设置为 application/x-7z-compressed。

标签: php linux file header transfer


【解决方案1】:

在您致电readfile($path) 之前,请先拨打ob_clean();flush();

所以最后你的代码应该是这样的:

private function pushToBrowser($file){
    if(!$file){ // file does not exist
        die('file not found');
    } else {
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: application/x-7z-compressed");
        header("Content-Transfer-Encoding: binary");
        header("Content-length: ".filesize($file).";\n");
        ob_clean();
        flush();
        // read the file from disk
        readfile($file);
    }
}

【讨论】:

    【解决方案2】:

    要提供 7 个 zip 文件,您的内容类型应为 application/x-7z-compressed

    如果您同时提供 7 个 zip 和 zip 或 rar 文件,那么您必须以编程方式将内容类型设置为浏览器。

    【讨论】:

      猜你喜欢
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2012-07-22
      • 2016-08-01
      相关资源
      最近更新 更多