Christian Giupponi Christian Giupponi - 1 month ago 14
PHP Question

Laravel and ZipArchive - nvalid or uninitialized Zip object

I need to create a zip using some file on my server.

I used Zipper from here but it does not allow me to add a custom name while adding a file so I switched on ZipArchive.

Here the code:

$zipper = new \ZipArchive();

foreach ($tracks as $track) {
$trackName = $track->name;
$trackPath = $customUploads->getCustomTrackFilePath($track, true);
$zipper->addFile($trackPath, $trackName);
}

$zipper->close();


Here the error:


ErrorException in PlaylistController.php line 223:
ZipArchive::addFile(): Invalid or uninitialized Zip object


I tried to add this control and when execute it I get the
dd()
message so looks like it is not able to create.

if( $zipper->open($zipName) !== true ){
dd('no');
}


The strange thing is that with the library linked at the top the zip is created.

Where is my error?

Answer

You are missing 2nd parameter flags of Zip::open function. Please check manual here http://php.net/manual/en/ziparchive.open.php

$zipper = $zip->open('test.zip', ZipArchive::CREATE);
Comments