簡體   English   中英

使用PHP下載后不提取.zip文件

[英]not extract .zip files after download using PHP

我使用這個PHP類下載.zip格式的后台文件:

public function download($file)
    {
        $filename = $this->dir . $file;
        $fp = fopen($filename, "rb");
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header("Content-length: " . filesize($filename));
        header("Content-disposition: attachment;filename = " . $file . "");
        header("Content-Transfer-Encoding: binary");
        readfile($filename);
        ob_end_clean();
        die(fpassthru($fp));
        fclose($fp);
    }

這工作,我下載.zip文件成功。 但是當我需要使用winrar提取.zip文件時,我看到了這個錯誤:

db-backup-2014-11-15_14-18-48-12.zip: The archive is either in unknown format or damaged

在下載中:

在此輸入圖像描述

在開放:

在此輸入圖像描述

在提取物中:

在此輸入圖像描述

注意:我的原始文件工作,並使用winrar精細提取。

我認為我的問題是CRC32 = 0000000(在屏幕一),因為在原始文件中有一個真值。

怎么解決這個問題?!

您的ZIP文件很可能已損壞,因為您的PHP腳本在關閉?>標簽后會包含空格,從而污染ZIP內容。 要解決這個問題,請從腳本中刪除PHP結束標記?> ,函數download()是其中的一部分(同時檢查是否沒有前導空格,在打開<?php標記之前放置)並且您的問題應該消失。 如果包含其他腳本,請檢查它們是否也有空格。 一般來說,不應該使用?>標簽,除非你確定你因為已知的原因需要它(這種情況非常罕見)。

請嘗試使用此下載功能:

<?php    
    public function download($file)  {
        ob_end_clean();
        $filename = $this->dir . $file;
        header("Content-Type: application/zip");
        header("Content-Disposition: attachment; filename=". pathinfo($filename , PATHINFO_BASENAME));
        header("Content-Length: " . filesize($filename ));
        readfile($fileName);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM