ChocCookieRaider
ChocCookieRaider

Reputation: 145

ZipArchive::close() Invalid or unitialized Zip object

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.

Upvotes: 7

Views: 14591

Answers (3)

tim
tim

Reputation: 2722

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();
}

Upvotes: 0

DrKey
DrKey

Reputation: 3495

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));
}

Upvotes: 0

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();

Upvotes: 6

Related Questions