PHP ZipArchive 未添加超过 700 个文件

发布于 2024-09-13 07:43:35 字数 1722 浏览 7 评论 0原文

我对 php_zip.dll 的 ZipArchive 类有疑问。我通过 php.net 上建议的 ZipArchiveImproved 包装类来使用它,以避免最大文件句柄问题。

问题其实很简单:700个文件添加正确(jpg图像文件),其余的失败。 addFile 方法返回 false。

PHP版本是5.2.6。

奇怪的是,这实际上曾经有效。
可能是什么问题?你能给我任何线索吗?

预先非常感谢您!

编辑:抱歉,我没有收到任何错误消息是不正确的(display_errors 在 php.ini 中被关闭,我之前没有注意到)。从 701. 文件开始,我收到以下错误消息:

Warning: ZipArchive::addFile() [ziparchive.addfile]: Invalid or unitialized Zip object in /.../includes/ZipArchiveImproved.class.php on line 104

看起来 close() 调用返回 false,但没有发出任何错误。有什么想法吗?

编辑2:相关来源:

include_once DIR_INCLUDES . 'ZipArchiveImproved.class.php';

ini_set('max_execution_time', 0);

$filePath = $_SESSION['fqm_archivePath'];

$zip = new ZipArchiveImproved();
if(! $zip->open($filePath, ZipArchive::CREATE))
{
    echo '<div class="error">Hiba: a célfájl a(z) "' . $filePath . '" útvonalon nem hozható létre.</div>';
    return;
}

echo('Starting (' . count($_POST['files']) . ' files)...<br>');

$addedDirs = array();
foreach($_POST['files'] as $i => $f)
{
    $d = getUserNameByPicPath($f);
    if(! isset($addedDirs[$d]))
    {
        $addedDirs[$d] = true;
        $zip->addEmptyDir($d);

        echo('Added dir "' . $d . '".<br>');
    }

    $addName = $d . '/' . basename($f);
    $r = $zip->addFile($f, $addName);
    if(! $r)
    {
        echo('<font color="Red">[' . ($i + 1) . '] Failed to add file "' . $f . '" as "' . $addName . '".</font><br>');
    }
}

$a = $zip->addFromString('test.txt', 'Moooo');
if($a)
{
    echo 'Added string successfully.<br>';
}
else
{
    echo 'Failed to add string.<br>';
}

$zip->close();

I have a problem with the php_zip.dll's ZipArchive class. I'm using it through the ZipArchiveImproved wrapper class suggested on php.net to avoid the max file-handle issue.

The problem is really simple: 700 files are added properly (jpg image files), and the rest fails. The addFile method returns false.

The PHP version is 5.2.6.

The weird thing is that this actually used to work.
What could be the problem? Can you give me any clues?

Thank you very much in advance!

Edit: sorry, it's not true that I'm not getting any error message (display_errors was switched off in php.ini I didn't notice it before). From the 701. file on, I'm getting the following error message:

Warning: ZipArchive::addFile() [ziparchive.addfile]: Invalid or unitialized Zip object in /.../includes/ZipArchiveImproved.class.php on line 104

Looks like the close() call returns false, but issues no error. Any ideas?

Edit 2: the relevant source:

include_once DIR_INCLUDES . 'ZipArchiveImproved.class.php';

ini_set('max_execution_time', 0);

$filePath = $_SESSION['fqm_archivePath'];

$zip = new ZipArchiveImproved();
if(! $zip->open($filePath, ZipArchive::CREATE))
{
    echo '<div class="error">Hiba: a célfájl a(z) "' . $filePath . '" útvonalon nem hozható létre.</div>';
    return;
}

echo('Starting (' . count($_POST['files']) . ' files)...<br>');

$addedDirs = array();
foreach($_POST['files'] as $i => $f)
{
    $d = getUserNameByPicPath($f);
    if(! isset($addedDirs[$d]))
    {
        $addedDirs[$d] = true;
        $zip->addEmptyDir($d);

        echo('Added dir "' . $d . '".<br>');
    }

    $addName = $d . '/' . basename($f);
    $r = $zip->addFile($f, $addName);
    if(! $r)
    {
        echo('<font color="Red">[' . ($i + 1) . '] Failed to add file "' . $f . '" as "' . $addName . '".</font><br>');
    }
}

$a = $zip->addFromString('test.txt', 'Moooo');
if($a)
{
    echo 'Added string successfully.<br>';
}
else
{
    echo 'Failed to add string.<br>';
}

$zip->close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

鸠书 2024-09-20 07:43:35

这可能是因为操作系统中打开文件的最大数量(请参阅 http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/ 了解更多详细信息,可以在系统范围内或仅在用户范围内;限制)。

Zip 使每个添加的文件保持打开状态,直到调用 Zip::close 为止。

解决方案是关闭并重新打开每个 X 文件的存档(256 或 512 应该是安全值)。

It's probably because of maximal number of open files in your OS (see http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/ for more detailed info; it can by system-wide or just user limit).

Zip keeps every added file open until Zip::close is called.

The solution is to close and reopen archive every X files (256 or 512 should be safe value).

路还长,别太狂 2024-09-20 07:43:35

该问题如下所述:http://www.php。 net/manual/en/function.ziparchive-open.php#88765

您是否尝试过指定这两个标志?

The problem is described here: http://www.php.net/manual/en/function.ziparchive-open.php#88765

Have you tried to specify both flags?

時窥 2024-09-20 07:43:35

我通过增加 ulimit 解决了这个问题:ulimit -n 8192

I solved this problem by increasing the ulimit: ulimit -n 8192.

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