简体   繁体   中英

not extract .zip files after download using PHP

I download my back file with .zip format using this PHP class:

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);
    }

this worked and i downloaded .zip file successful. but when i need to extract .zip file using winrar i see this error:

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

in download :

在此输入图像描述

in open:

在此输入图像描述

in extract :

在此输入图像描述

NOTE: my original file worked and fine extracted using winrar.

i think my problem is CRC32 = 0000000 (in screen one) because in original file is have a true value.

how do fix this problem ?!

Your ZIP file is most likely corrupted because your PHP script contains white space after closing ?> tag which pollutes ZIP content. To solve this, remove PHP closing tag ?> from your script that function download() is part of (also check if you do not have leading white-spaces, placed before opening <?php tag too) and your issue should be gone. If you include other scripts, check them for spaces too. In general ?> tag should not be used, unless you are sure you need it it for known reason (which is very rare case).

Try this download function instead :

<?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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM