ZipArchive::close() Invalid or unitialized Zip object
Asked Answered
S

3

7

I'm trying to backup my site by zipping it all, and putting the zip into an unnaccessible folder, done with PHP. My code is

<?php
Zip('../../', './');
function Zip($source, $destination)
{
    if (extension_loaded('zip') === true)
    { echo'a';
        if (file_exists($source) === true)
        {
            $zip = new ZipArchive();

            if ($zip->open($destination, ZIPARCHIVE::CREATE) === true)
            {
                $source = realpath($source);

                if (is_dir($source) === true)
                {
                    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

                    foreach ($files as $file)
                    {
                        $file = realpath($file);

                        if (is_dir($file) === true)
                        {
                            $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                        }

                        else if (is_file($file) === true)
                        {
                            $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                        }
                    }
                }

                else if (is_file($source) === true)
                {
                    $zip->addFromString(basename($source), file_get_contents($source));
                }
            }

            return $zip->close(); // The error.
        }
    }

    return false;
}
?>

But I get an error of Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in backup.php on line 41 I have searched google, and no results.

Sudra answered 6/12, 2012 at 22:10 Comment(1)
Does anyone have any idea the cause of this?Sudra
C
6

From PHP 5.2.8, this issue has started to emerge.

Try adding the FLAGS to the open method

 - ZipArchive::OVERWRITE
 - ZipArchive::CREATE
 - ZipArchive::EXCL
 - ZipArchive::CHECKCONS

This command would most probably fix the issue

$zip->open($destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);

Quick and Dirtiest Fix would be this, if the above doesn't work

@$zip->close();
Cyma answered 31/7, 2013 at 15:49 Comment(2)
what's the '@' do in @$zip->close()? adding the two flags you mentioned worked for me. +1Lofty
@fungusanthrax, It is an error supression operator. php.net/manual/en/language.operators.errorcontrol.phpCyma
F
0

In my case the destination folder was completely missing and this was causing the error on close().

Therefore I check if the destination folder exists and, if not, I try to create it. If both calls fail, I throw an exception. In your case would be something like the following:

$destinationPath = (new \SplFileInfo($destination))->getPath();
if (!is_dir($destinationPath) && !mkdir($destinationPath, 0755, true)) {
    throw new \Exception(sprintf('Destination folder "%s" is missing and cannot be created', $destinationPath));
}
Functionalism answered 13/12, 2021 at 16:28 Comment(0)
S
0

The error is in regards to closing a zip that failed to open. Attempt to close it only if it was successfully opened.

$zip = new ZipArchive();

if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) {

  ...

  $zip->close();
}
Scripture answered 29/7 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.