1

I am using ziparchive to create a zip for some files and then need to download the file using the following code.

if(count($valid_files > 0)){
    $zip = new ZipArchive();
    $zip_name = "pixels.zip";
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
        $error .= "* Sorry ZIP creation failed at this time";
    }

    foreach($valid_files as $file){
        $zip->addFile($file);
    }

    $zip->close();
    if(file_exists($zip_name)){
        // force to download the zip
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private",false);
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
        ob_clean();
        flush();
        readfile($zip_name);
     //remove zip file from temp path
        unlink($zip_name);
    }

} else {
    echo "No valid files to zip";
    exit;
}

Assuming that $valid_files contains an array with each element as the complete file path. The above code is expected to create a zip file containing the files but on the contrary once the download is initiated it keeps on downloading without any finishing size or maximum size. And in some cases even if the file is downloaded then when we try to unzip the file it gives an error as invalid file. Unable to figure out the issue any help is greatly appreciated.

OshoParth
  • 1,492
  • 2
  • 20
  • 44
  • `$error` doesn't contain anything? – Jaime Dec 12 '17 at 19:38
  • So sometimes a file is downloaded at least? What happens if you remove the `unlink` and compare those two files? Are they the same? Can the server-side file be unziped? Your snippet seems to work okish for me. – leberknecht Dec 12 '17 at 19:39
  • The browser will show a progress bar like you expect if add a header like header("Content-length: $ size"); – Marcelo Staudt Dec 12 '17 at 20:20
  • ZIP-files are binary. You need to add a `Content-Transfer-Encoding: binary` header to make sure the file is not served as a (malformed) text file. – rickdenhaan Dec 12 '17 at 20:26
  • Issue remains the same the file keeps on downloading for infinite size with no total size description even after adding the headers as suggested by Marcelo and @rickdenhaan. – OshoParth Dec 13 '17 at 02:57
  • @delf the error log gives the following error : PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 132136960 bytes) in /home2/site_name/public_html/my_gallery.php on line 124 – OshoParth Dec 13 '17 at 03:01

1 Answers1

0

The major reason for this was that there was huge zip file already existing in the same folder with the zip name I used to create a new zip thus due to some reason the new zip was not created and this old huge zip begined to download and as suggested by Marcelo in the comments the size was not mentioned thus downloading wasn't successful but things worked when in declared encoding as binary mentioned the size and changed the zip name or deleted the previous zip file.

OshoParth
  • 1,492
  • 2
  • 20
  • 44