在尝试压缩主机中文件夹的内容以更容易地迁移数据(VPS,没有花哨的界面面板)时,脚本抛出一个我无法解码的错误。我总是可以从命令行压缩内容并使用它,但我想为我的客户创建一个简单的界面,以便在任何时候一键下载我的作品。下面是错误:
Warning: Unknown: Cannot destroy the zip context in Unknown on line 0脚本:
define('DS', DIRECTORY_SEPARATOR);
define('DR', realpath(dirname(__FILE__)).DS);
if (!isset($_GET['folder']))
{
die('folder missing');
}
$rootPath = DR.$_GET['folder'].DS;
// Initialize archive object
$zip = new ZipArchive();
$zip->open(DR.'backup'.DS.'backup.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();有没有猜到?
发布于 2015-07-09 02:50:09
我刚刚在我自己的一个项目中解决了同样的错误信息...不知道是不是同样的问题,但在我的例子中,我没有创建要添加zip的文件夹,所以它会抛出这个错误。一旦我添加了文件夹,一切都很正常。希望这能有所帮助。
发布于 2019-09-16 22:31:29
我遇到了与@AlexW.H.B相同的问题;ZipArchive的open方法可能会打开包含不存在的文件夹的zip文件名,所以我尝试在zip中创建任何文件,但php-fpm子进程在尝试写入zip目标时被终止,没有明显的错误消息。查看php-fpm日志后,错误如下:
Warning: Unknown: Cannot destroy the zip context in Unknown on line 0实际上,ZipArchive或php-fpm应该抛出一个带有清晰错误消息的异常,它可以在PHP代码中捕获。
https://stackoverflow.com/questions/30306946
复制相似问题