Php ZipArchive::close()无效或统一化的Zip对象

Php ZipArchive::close()无效或统一化的Zip对象,php,zip,Php,Zip,我正试图通过压缩所有内容并将压缩文件放入一个无法访问的文件夹来备份我的站点,这是用PHP完成的。我的代码是 <?php Zip('../../', './'); function Zip($source, $destination) { if (extension_loaded('zip') === true) { echo'a'; if (file_exists($source) === true) { $zip =

我正试图通过压缩所有内容并将压缩文件放入一个无法访问的文件夹来备份我的站点,这是用PHP完成的。我的代码是

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


但是我在第41行的backup.php中得到了一个错误:ZipArchive::close()[ZipArchive.close]:无效或统一的Zip对象。我搜索了google,没有结果。

php 5.2.8开始,这个问题开始出现

尝试将
标志添加到
open
方法中

 - ZipArchive::OVERWRITE
 - ZipArchive::CREATE
 - ZipArchive::EXCL
 - ZipArchive::CHECKCONS
这个命令很可能会解决这个问题

$zip->open($destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
如果上述方法不起作用的话,最快捷、最肮脏的解决方法就是这个

@$zip->close();

有人知道这件事的原因吗?