Alex Alex - 30 days ago 5
PHP Question

ZIP file not being created, but no error gets triggered

I'm using ZipArchive:

function zip_dir($source, $target){

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);

$zip = new \ZipArchive();
if($zip->open($target, \ZipArchive::CREATE) !== true)
exit('cannot create zip');

foreach($iterator as $file){
print $file . '<br>';

return $target;

zip_dir(__DIR__ . '/test/', __DIR__ . '/');

I can see the list of files, but in the end I cannot find the zip file that's supposed to be created. And I get no errors / exceptions from ZipArchive...


I've added
print $zip->getStatusString();

and it prints :Can't open file: Permission denied".
What does that mean? I know for sure every directory is writable, bc I can create new files with PHP inside them...

edit 2:

print 'target dir is writable...';

it prints that, so the dir is writable. Feels like I'm in the twilight zone...

Answer Source

Two Comments From

If you're adding multiple files to a zip and your $zip->close() call is returning FALSE, ensure that all the files you added actually exist. Apparently $zip->addFile() returns TRUE even if the file doesn't actually exist. It's a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.


Don't forget to check the zip isn't empty, folks - otherwise the zip won't be created at all, and the server will issue no warning!