[英]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.我正在尝试通过压缩所有内容来备份我的网站,并将 zip 放入一个无法访问的文件夹中,用 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.但是我
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
我搜索了谷歌,但没有结果。
I'm trying to backup my site by zipping it all, and putting the zip into an unnaccessible folder, done with PHP.我正在尝试通过全部压缩并将网站的zip压缩到不可访问的文件夹(使用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.但是我收到
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,但没有结果。
In my case the destination folder was completely missing and this was causing the error on close()
.在我的情况下,目标文件夹完全丢失,这导致了
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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.