代码之家  ›  专栏  ›  技术社区  ›  Spyros Savvanis

php头文件传输已损坏

  •  1
  • Spyros Savvanis  · 技术社区  · 9 年前

    我在文件夹里有一些文件。我使用以下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);
    
    2 回复  |  直到 9 年前
        1
  •  1
  •   Markus    9 年前

    在你打电话之前 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
  •  0
  •   ARIF MAHMUD RANA    9 年前

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

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