Search code examples
phpapacheerror-handlingzip

ERR_EMPTY_RESPONSE on downloading generated zip


So i was trying to generate a zip file with a bunch of pdfs in it.

After some time I got it working but the problem servers gives me ERR_EMPTY_RESPONSE.

  • Server's apache is 2.2 on windows server 2003...
  • PHP Version is 5.2.

This is the code:

<?php
$file_names=$pdfs; //This is an array of names of files in PDF format: 1.pdf, 2pdf, 3pdf...


$zip = new ZipArchive;
$zip_name = ("test.zip");
$path_zip = ("C:/www/test/docs/");
$zip->open($path_zip.$zip_name,ZipArchive::CREATE);

foreach ($file_names as $file) {
    $zip->addFile($file,basename($file));
}

$zip->close();

//then send the headers to force download the zip file

header("Content-type: application/zip"); 
header("Content-Disposition: attachment; filename=$zip_name"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
@readfile($path_zip.$zip_name);
exit;
?>

THE PDFs ARE PREVIOUSLY DOWNLOADED FROM THE INTERNET WITH ANOTHER CODE

How do I fix this??

EDIT::

Adding a image showing the zip. Sizes are not real.

EDIT 2:: I removed the @ at the last line and tested. Nothing works as intended.


Solution

  • So...

    It's ugly but it works...

    I just changed

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$zip_name"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile($path_zip.$zip_name);
    exit;
    

    for

    header("Location: ".$path_zip.$zip_name);
    

    As I say, it's pretty ugly, but it works, it make the user download the file and it doesn't get stuck or something like that.

    If someone want to make another true fix, I will test it, as I don't want to change the location.

    Bye.