Christian Giupponi Christian Giupponi - 1 year ago 276
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);


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
message so looks like it is not able to create.

if( $zipper->open($zipName) !== true ){

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

Where is my error?

Answer Source

You are missing 2nd parameter flags of Zip::open function. Please check manual here

$zipper = $zip->open('', ZipArchive::CREATE);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download