繁体   English   中英

ZipArchive::close() 无效或未初始化的 Zip 对象

[英]ZipArchive::close() Invalid or unitialized Zip object

我正在尝试通过压缩所有内容来备份我的网站,并将 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;
}
?>

但是我Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in backup.php on line 41收到错误Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in backup.php on line 41我搜索了谷歌,但没有结果。

我正在尝试通过全部压缩并将网站的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;
}
?>

但是我收到Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in backup.php on line 41错误Warning: ZipArchive::close() [ziparchive.close]: Invalid or unitialized Zip object in backup.php on line 41我已经搜索了google,但没有结果。

在我的情况下,目标文件夹完全丢失,这导致了close()错误。

因此,我检查目标文件夹是否存在,如果不存在,则尝试创建它。 如果两个调用都失败,我会抛出异常。 在您的情况下,将类似于以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM