创建 zip 时没有错误,但未创建
我编写此代码是为了创建一个 ZIP 文件并保存它。但不知怎的,它只是没有显示任何错误,但它也没有创建 ZIP 文件。这是代码:
$zip = new ZipArchive;
$time = microtime(true);
$res = $zip->open("maps/zips/test_" . $time . ".zip", ZipArchive::CREATE);
if ($res === TRUE) {
echo "RESULT TRUE...";
$zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
$zip->addFromString('how_to_install.txt', 'Some Explanation...');
$zip->close();
$zip_created = true;
echo "FILE ADDED!";
}
我做错了什么,如何修复它?
I wrote this code to create a ZIP file and to save it. But somehow it just doesn't show any error, but it doesn't create a ZIP file either. Here's the code:
$zip = new ZipArchive;
$time = microtime(true);
$res = $zip->open("maps/zips/test_" . $time . ".zip", ZipArchive::CREATE);
if ($res === TRUE) {
echo "RESULT TRUE...";
$zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
$zip->addFromString('how_to_install.txt', 'Some Explanation...');
$zip->close();
$zip_created = true;
echo "FILE ADDED!";
}
What am I doing wrong, and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可能 apache 或 php 无权在该目录中创建 zip 存档。来自 ZipArchice::open 的评论之一:
在 if 语句中添加 else 子句并转储 $res 以查看结果:
Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on ZipArchice::open:
Add an else clause to your if statement and dump $res to see the results:
有两种情况 zip 不会生成错误。
当调用
zip->close
然后存档时,一个文件不可用将会失败并且您的 zip 文件不会被创建。
有写权限的zip就不会报错了。它将完成
但什么也不会被创造。
There are 2 cases when zip doesn't generate the error.
one file is not available when
zip->close
is called then the archivewill fail and your zip file won't be created.
have write permissions zip will not report the error. It will finish
but nothing will be created.
即使具有完全的写入/读取权限,我也遇到了完全相同的问题。
通过在将“.zip”文件传递给 ZipArchive 之前手动创建“.zip”文件来解决:
I had an exactly same issue, even when with full writing/reading permissions.
Solved by creating the ".zip" file manually before passing it to ZipArchive:
在调用 $zip->addFile 之前检查每个文件是否存在,否则将不会生成 zip 并且不会显示错误消息。
Check out that each of your file exists before calling $zip->addFile otherwise the zip won't be generated and no error message will be displayed.
将其分解为步骤。
检查 phpinfo 是否启用了 zip :)
break it into steps.
Check the phpinfo for zip is enabled or not :)
未创建 zip 文件的原因之一是缺少检查是否添加文件而不是目录。
我在此处找到了解决方案。
One of the reasons for zip file is not created is due to missing check if you are adding file and not a directory.
I found the solution here.