PHP ZipArchive 无法破坏 zip 上下文

yod*_*oda 3 php zip

在尝试压缩主机中文件夹的内容以更轻松地迁移数据时(VPS,没有花哨的界面面板),脚本向我抛出一个我无法解码的错误。我总是可以从命令行压缩内容并使用它,但我想为我的客户创建一个简单的界面,以便随时单击下载我的作品。这是错误:

Warning: Unknown: Cannot destroy the zip context in Unknown on line 0
Run Code Online (Sandbox Code Playgroud)

剧本 :

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();
Run Code Online (Sandbox Code Playgroud)

有什么猜测吗?

Ale*_*.B. 5

我刚刚在我自己的一个项目中解决了相同的错误消息......不知道它是否是相同的问题,但在我的情况下,我没有创建要添加 zip 的文件夹,因此它会抛出该错误。添加文件夹后,一切正常。希望有帮助。