[英]ZipArchive::getStatusString(): Invalid or uninitialized Zip object
The following code will fail to create a ZIP file in PHP 5.6.12 and will also fail to print out the ZIP error message, instead displaying the error / warning 以下代码将无法在PHP 5.6.12中创建ZIP文件,也将无法打印出ZIP错误消息,而是显示错误/警告
Warning: ZipArchive::getStatusString(): Invalid or uninitialized Zip object in /tmp/x.php on line 9
But why? 但为什么? This once worked in PHP 5.4.
这曾经在PHP 5.4中起作用。
<?php
// TODO: Check for errors
$tempPath = tempnam('/tmp', 'ztmp');
$zip = new ZipArchive();
$res = $zip->open($tempPath, ZipArchive::OVERWRITE | ZipArchive::CREATE | ZipArchive::EXCL);
if ( $res !== true )
die($zip->getStatusString()."\n");
It looks like the semantics have changed somewhat; 语义似乎有所改变; it is unclear whether it is deliberate or a bug.
目前尚不清楚这是故意的还是错误的。
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. 无论如何,问题在于我们有一个空文件,该文件不是有效的ZIP文件,但是尽管我们要求覆盖该文件,但它仍在打开且未正确初始化。
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");
I know this is now one year old, but what I noticed is that here ZipArchive::EXCL is used. 我知道现在已经一岁了,但是我注意到这里使用了ZipArchive :: EXCL。
http://php.net/manual/en/zip.constants.php http://php.net/manual/en/zip.constants.php
ZipArchive::EXCL (integer)
ZipArchive :: EXCL (整数)
Error if archive already exists.如果存档已存在,则错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.