创建 zip 时没有错误,但未创建

发布于 2024-10-11 22:27:25 字数 521 浏览 5 评论 0原文

我编写此代码是为了创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

灼痛 2024-10-18 22:27:25

可能 apache 或 php 无权在该目录中创建 zip 存档。来自 ZipArchice::open 的评论之一:

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

相反,请确保您收集了
ZipArchive::close() 的返回值。
如果它是假的...它就不起作用。

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

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

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);
}
作妖 2024-10-18 22:27:25

有两种情况 zip 不会生成错误。

  1. 确保添加到 zip 中的每个文件都有效。即使
    当调用
    zip->close然后存档时,一个文件不可用
    将会失败并且您的 zip 文件不会被创建。
  2. 如果您的文件夹没有
    有写权限的zip就不会报错了。它将完成
    但什么也不会被创造。

There are 2 cases when zip doesn't generate the error.

  1. 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.
  2. If your folder doesn't
    have write permissions zip will not report the error. It will finish
    but nothing will be created.
好倦 2024-10-18 22:27:25

即使具有完全的写入/读取权限,我也遇到了完全相同的问题。

通过在将“.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!";
}

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 = 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!";
}
℡寂寞咖啡 2024-10-18 22:27:25

在调用 $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';
}

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.

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';
}
千纸鹤带着心事 2024-10-18 22:27:25

将其分解为步骤。

if ($res === TRUE) {

  check if file_exist

check if addFile give any error
}

if($zip->close())
{
 $zip_created = true; 
    echo "FILE ADDED!"
}

检查 phpinfo 是否启用了 zip :)

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 :)

狼亦尘 2024-10-18 22:27:25

未创建 zip 文件的原因之一是缺少检查是否添加文件而不是目录。

if (!$file->isDir())

我在此处找到了解决方案。

One of the reasons for zip file is not created is due to missing check if you are adding file and not a directory.

if (!$file->isDir())

I found the solution here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文