且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-22 18:04:39

看起来语义有所改变;不清楚是故意的还是bug.

It looks like the semantics have changed somewhat; it is unclear whether it is deliberate or a bug.

无论如何,问题是我们有一个空文件,它不是一个有效的 ZIP,但它仍然被打开并且没有正确初始化,即使我们请求覆盖该文件.

Anyways, the problem is that we have an empty file which is not a valid ZIP but which is being opened nonetheless and not initialized properly even though we requested the file to be overwritten.

所以解决方法或修复是删除现有文件,然后重新创建它:

So the workaround or fix is to delete the existing file and then recreate it:

<?php

$tempPath = tempnam('/tmp', 'ztmp');

// Delete first
@unlink($tempPath);

$zip = new ZipArchive();
$res = $zip->open($tempPath, ZipArchive::OVERWRITE | ZipArchive::CREATE | ZipArchive::EXCL);

if ( $res !== true )
    die($zip->getStatusString()."\n");