Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid or unitialized Zip object

Tags:

php

zip

if(isset($_POST['items'])) {
    // open zip
    $zip_path = 'downloadSelected/download.zip';
    $zip = new ZipArchive();

    if ($zip->open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
       die ("An error occurred creating your ZIP file.");
     }

     foreach ($_POST['items'] as $path) {
        // generate filename to add to zip
        $filepath = 'downloads/' . $path . '.zip';
        if (file_exists($filepath)) {
                $zip->addFile($filepath, $path . '.zip') or die ("ERROR: Could not add the file $filename");
        }
        else {
                die("File $filepath doesnt exit");
                }
        $zip->close();
       } }

I am getting Warning: ZipArchive::addFile() [function.ZipArchive-addFile]: Invalid or unitialized Zip object. what could be problem? I tried many methods but in vain. I am able to create and initiate download when I select one file. However, I get above error when I select more than one file.

like image 265
user3001378 Avatar asked Nov 17 '13 10:11

user3001378


2 Answers

the library itself gives you a code to see why it fails:

$ZIP_ERROR = [
  ZipArchive::ER_EXISTS => 'File already exists.',
  ZipArchive::ER_INCONS => 'Zip archive inconsistent.',
  ZipArchive::ER_INVAL => 'Invalid argument.',
  ZipArchive::ER_MEMORY => 'Malloc failure.',
  ZipArchive::ER_NOENT => 'No such file.',
  ZipArchive::ER_NOZIP => 'Not a zip archive.',
  ZipArchive::ER_OPEN => "Can't open file.",
  ZipArchive::ER_READ => 'Read error.',
  ZipArchive::ER_SEEK => 'Seek error.',
];

$result_code = $zip->open($zip_fullpath);
if( $result_code !== true ){
   $msg = isset($ZIP_ERROR[$result_code])? $ZIP_ERROR[$result_code] : 'Unknown error.';
   return ['error'=>$msg];
}
like image 198
Alejandro Silva Avatar answered Nov 05 '22 00:11

Alejandro Silva


According to the documentation you cannot OR the open mode flags, you can only open in one mode. Check the file exists first and set the mode on open appropriately and see if that helps.

like image 29
Rob Baillie Avatar answered Nov 04 '22 22:11

Rob Baillie