php 无效或未初始化的 Zip 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20029606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid or unitialized Zip object
提问by user3001378
if(isset($_POST['items'])) {
// open zip
$zip_path = 'downloadSelected/download.zip';
$zip = new ZipArchive();
if ($zip->open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
die ("An error occurred creating your ZIP file.");
}
foreach ($_POST['items'] as $path) {
// generate filename to add to zip
$filepath = 'downloads/' . $path . '.zip';
if (file_exists($filepath)) {
$zip->addFile($filepath, $path . '.zip') or die ("ERROR: Could not add the file $filename");
}
else {
die("File $filepath doesnt exit");
}
$zip->close();
} }
I am getting Warning: ZipArchive::addFile()[function.ZipArchive-addFile]: Invalid or unitialized Zip object. what could be problem? I tried many methods but in vain.
I am able to create and initiate download when I select one file. However, I get above error when I select more than one file.
我收到警告:ZipArchive::addFile()[function.ZipArchive-addFile]: Invalid or unitialized Zip object。可能有什么问题?我尝试了很多方法,但徒劳无功。当我选择一个文件时,我能够创建和启动下载。但是,当我选择多个文件时,出现上述错误。
回答by Rob Baillie
The power of indenting correctly!
正确缩进的力量!
You are closing your zip file inside your loop.
您正在关闭循环中的 zip 文件。
If I re-format your code it becomes obvious.
如果我重新格式化您的代码,它变得显而易见。
Corrected code follows:
更正后的代码如下:
if(isset($_POST['items'])) {
// open zip
$zip_path = 'downloadSelected/download.zip';
$zip = new ZipArchive();
if ($zip->open($zip_path, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== TRUE) {
die ("An error occurred creating your ZIP file.");
}
foreach ($_POST['items'] as $path) {
// generate filename to add to zip
$filepath = 'downloads/' . $path . '.zip';
if (file_exists($filepath)) {
$zip->addFile($filepath, $path . '.zip') or die ("ERROR: Could not add the file $filename");
} else {
die("File $filepath doesnt exit");
}
}
$zip->close();
}
回答by Alejandro Silva
the library itself gives you a code to see why it fails:
库本身为您提供了一个代码来查看它失败的原因:
$ZIP_ERROR = [
ZipArchive::ER_EXISTS => 'File already exists.',
ZipArchive::ER_INCONS => 'Zip archive inconsistent.',
ZipArchive::ER_INVAL => 'Invalid argument.',
ZipArchive::ER_MEMORY => 'Malloc failure.',
ZipArchive::ER_NOENT => 'No such file.',
ZipArchive::ER_NOZIP => 'Not a zip archive.',
ZipArchive::ER_OPEN => "Can't open file.",
ZipArchive::ER_READ => 'Read error.',
ZipArchive::ER_SEEK => 'Seek error.',
];
$result_code = $zip->open($zip_fullpath);
if( $result_code !== true ){
$msg = isset($ZIP_ERROR[$result_code])? $ZIP_ERROR[$result_code] : 'Unknown error.';
return ['error'=>$msg];
}
回答by Rob Baillie
According to the documentation you cannot OR the open mode flags, you can only open in one mode. Check the file exists first and set the mode on open appropriately and see if that helps.
根据文档你不能或打开模式标志,你只能在一种模式下打开。首先检查文件是否存在并适当地设置打开模式,看看是否有帮助。

