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.