1

I tried two ways for adding files to ZIP. But none of them are working. Downloaded ZIP size is 0 Bytes. I have the following code to add files from uploads/Applications folder to the ZIP archive.

$zip = new ZipArchive();
$zip_name = "REU_Files.zip";

if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
  die("Sorry ZIP creation failed at this time");

// first try

$handle = opendir("uploads/Applications/");
while(false !== ($file = readdir($handle)))
  if ($file != "." && $file != "..")
  {
    $zip->addFile("uploads/Applications/" . $file);
  }

// Second try

$zip->addEmptyDir("Applications/");
foreach (glob("uploads/Applications/*") as $file) {
  $zip->addFile($file, "Applications/" . basename($file));
}

$zip->close();

// Making zip downloadable

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);

Please help me finding where my code went wrong. Thanks in advance.

  • is output buffering on? check with ob_get_level(). also, have you tried removing the unlink()? maybe it removes the file before the client actualy have downloaded the file. – Louis Loudog Trottier Feb 05 '17 at 09:46
  • see http://stackoverflow.com/questions/1754352/download-multiple-files-as-zip-in-php?rq=1 – Louis Loudog Trottier Feb 05 '17 at 09:47
  • 1
    Thanks for your reply Louis. I really appreciate it. I resolved the issue. The problem is not with the code but with the folder permissions on the server. The folder does not have write permissions, so the zip file is not being created to be available for download. – user3273925 Feb 07 '17 at 12:38

0 Answers0