创建 zip 时没有错误,但没有创建 [英] No error when creating zip, but it doesn't get created

查看:34
本文介绍了创建 zip 时没有错误,但没有创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写这段代码是为了创建一个 ZIP 文件并保存它.但不知何故,它只是没有显示任何错误,但也没有创建 ZIP 文件.代码如下:

$zip = 新的 ZipArchive;$time = microtime(true);$res = $zip->open("maps/zips/test_" . $time . ".zip", ZipArchive::CREATE);如果($res === TRUE){echo "结果为真...";$zip->addFile("maps/filename.ogz","filename.ogz");//Sauerbraten地图格式$zip->addFromString('how_to_install.txt', '一些解释...');$zip->close();$zip_created = 真;echo "文件已添加!";}

我做错了什么,我该如何解决?

解决方案

可能是 apache 或 php 没有在该目录中创建 zip 存档的权限.来自对 ZipArchice::open 的评论之一:><块引用>

如果您正在编写的目录或存入没有正确的权限设置,你不会得到任何错误消息,它看起来像一切正常......除了它不会改变!

相反,请确保您收集了ZipArchive::close() 的返回值.如果它是假的......它没有用.

在 if 语句中添加 else 子句并转储 $res 以查看结果:

if($res === TRUE) {...} 别的 {var_dump($res);}

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?

解决方案

Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on 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.

Add an else clause to your if statement and dump $res to see the results:

if($res === TRUE) {
    ...
} else {
    var_dump($res);
}

这篇关于创建 zip 时没有错误,但没有创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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