简体   繁体   English

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

[英]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.

相关问题 codeigniter phpexcel error ZipArchive::getFromName(): Invalid or uninitialized Zip object - codeigniter phpexcel error ZipArchive::getFromName(): Invalid or uninitialized Zip object PHP ZipArchive 在提取时给出无效或未初始化 Zip object 错误 - PHP ZipArchive gives Invalid or uninitialized Zip object error on extraction Laravel和ZipArchive-nvalid或未初始化的Zip对象 - Laravel and ZipArchive - nvalid or uninitialized Zip object ZipArchive::close() 无效或未初始化的 Zip 对象 - ZipArchive::close() Invalid or unitialized Zip object PHPExcel警告:ZipArchive :: getFromName():无效或统一的Zip对象 - PHPExcel Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in Laravel 5-PHP警告:ZipArchive :: extractTo():无效或单元化的Zip对象 - Laravel 5 - PHP Warning: ZipArchive::extractTo(): Invalid or unitialized Zip object Google App Engine上的PhpExcel错误:无效或未初始化的Zip对象 - PhpExcel on Google App Engine Error: Invalid or uninitialized Zip object joomla ZipArchive产生无效的zip文件 - joomla ZipArchive producing invalid zip files PHP ZipArchive正在运行,但现在创建无效的zip文件 - PHP ZipArchive Was working, but now creates invalid zip files 将CRON与ZIPArchive对象一起使用以创建zip文件-拒绝小名次 - using CRON with ZIPArchive object to create zip file— pemmisions denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM