ZipArchive::getStatusString(): 无效或未初始化的 Zip 对象 [英] ZipArchive::getStatusString(): Invalid or uninitialized Zip object

查看:43
本文介绍了ZipArchive::getStatusString(): 无效或未初始化的 Zip 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将无法在 PHP 5.6.12 中创建 ZIP 文件,并且也无法打印出 ZIP 错误消息,而是显示错误/警告

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

Warning: ZipArchive::getStatusString(): Invalid or uninitialized Zip object in /tmp/x.php on line 9

但是为什么呢?这曾经在 PHP 5.4 中有效.

But why? This once worked in 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");

推荐答案

看起来语义有所改变;不清楚是故意的还是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");

这篇关于ZipArchive::getStatusString(): 无效或未初始化的 Zip 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆