PHP ZipArchive无法销毁zip上下文。

4

当我试图将主机文件夹的内容压缩以便更轻松地迁移数据(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();

任何猜测?
2个回答

6

我刚刚在自己的项目中解决了同样的错误信息...不知道是否是相同的问题,但在我的情况下,我没有创建zip文件被添加到的文件夹,所以它会抛出那个错误。一旦我添加了文件夹,一切都正常工作了。希望这可以帮助。


2

我遇到了与@AlexW.H.B相同的问题; ZipArchive的open方法可以打开包含不存在文件夹的zip文件名称,因此我尝试将任何文件创建到zip中,但是在尝试写入zip目标时,php-fpm子进程会被杀死,并且没有清晰的错误消息。在查看php-fpm日志后,发现错误出在这里:

Warning: Unknown: Cannot destroy the zip context in Unknown on line 0

实际上,ZipArchive或php-fpm应该抛出一个带有清晰错误信息的异常,可以在PHP代码中捕获。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接