php 创建 zip 时没有错误,但没有创建
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4651276/
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
No error when creating zip, but it doesn't get created
提问by Florian Müller
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 文件并保存它。但不知何故,它只是没有显示任何错误,但也没有创建 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!";
}
What am I doing wrong, and how can I fix it?
我做错了什么,我该如何解决?
采纳答案by alexn
Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on ZipArchice::open:
可能 apache 或 php 没有在该目录中创建 zip 档案的权限。来自ZipArchice::open的评论之一:
If the directory you are writing or saving into does not have the correct permissions set, you won't get any error messages and it will look like everything worked fine... except it won't have changed!
Instead make sure you collect the return value of ZipArchive::close(). If it is false... it didn't work.
如果您正在写入或保存的目录没有设置正确的权限,您将不会收到任何错误消息,并且看起来一切正常……除非它不会改变!
相反,请确保您收集了 ZipArchive::close() 的返回值。如果它是假的......它没有用。
Add an else clause to your if statement and dump $res to see the results:
在 if 语句中添加 else 子句并转储 $res 以查看结果:
if($res === TRUE) {
...
} else {
var_dump($res);
}
回答by user1720209
There are 2 cases when zip doesn't generate the error.
有两种情况 zip 不会产生错误。
- Make sure every file you are adding to the zip is valid. Even if
one file is not available when
zip->close
is called then the archive will fail and your zip file won't be created. - If your folder doesn't have write permissions zip will not report the error. It will finish but nothing will be created.
- 确保您添加到 zip 中的每个文件都是有效的。即使在
zip->close
调用时一个文件不可用,存档也会失败并且不会创建您的 zip 文件。 - 如果您的文件夹没有写入权限,则 zip 不会报告错误。它将完成,但不会创建任何内容。
回答by Pascal Messana
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.
在调用 $zip->addFile 之前检查您的每个文件是否存在,否则将不会生成 zip 并且不会显示任何错误消息。
if(file_exists($fichier->url))
{
if($zip->addFile($fichier->url,$fichier->nom))
{
$erreur_ouverture = false;
}
else
{
$erreur_ouverture = true;
echo 'Open error : '.$fichier->url;
}
}
else
{
echo 'File '.$fichier->url.' not found';
}
回答by Bob
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”文件传递给 ZipArchive 之前手动创建“.zip”文件来解决:
$zip = new ZipArchive;
$time = microtime(true);
$path = "maps/zips/test_" . $time . ".zip"
touch($path); //<--- this line creates the file
$res = $zip->open($path, 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!";
}
回答by Vinaykumar Patel
回答by zod
break it into steps.
把它分成几个步骤。
if ($res === TRUE) {
check if file_exist
check if addFile give any error
}
if($zip->close())
{
$zip_created = true;
echo "FILE ADDED!"
}
Check the phpinfo for zip is enabled or not :)
检查 phpinfo 是否启用 zip :)