Iam getting crazy with PHP 5.2x and “ZipArchive”, the following code does not throw an error, every part returns true, but no ZIP File is created.
Does someone can see an error iam not seeing?
if ($handle = opendir($directory)) {
$zip = new ZipArchive();
$filename = time() . '.zip';
$path = APPLICATION_PATH . '/../download/';
$status = $zip->open($path . $filename, ZIPARCHIVE::CREATE);
if ($status !== true) {
$this->log->err('Cant create zipArchive!');
return false;
}
while (false !== ($file = readdir($handle))) {
if (!is_dir($file)) {
if (is_readable($file)) {
if (!$zip->addFile($file)) {
$this->log->err('Cant add File to archive: ' . $file);
}
} else {
$this->log->debug('Added file to archive: ' . $file);
}
}
}
closedir($handle);
if (!$zip->close()) {
$error = $zip->getStatusString();
$this->log->err($error);
$this->log->err('Cant create archive..');
}
}
You never actually add any file in your ZIP archive. It’s the only reason you have no errors.
is_readable($file)always returnfalse, and when it doesAdded file to archive ...is printed, probably because you misplaced yourelseblock.You have to add the folder to
$file, in your case it should be$directory: