Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

not extract .zip files after download using PHP

Tags:

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 :

enter image description here

in open:

enter image description here

in extract :

enter image description here

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 ?!

like image 256
Pink Code Avatar asked Nov 16 '14 15:11

Pink Code


2 Answers

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);
    }
like image 135
DarkBee Avatar answered Sep 22 '22 18:09

DarkBee


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).

like image 34
Marcin Orlowski Avatar answered Sep 20 '22 18:09

Marcin Orlowski