简体   繁体   中英

PHP ZipArchive not adding more than 700 files

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();

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

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

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

Have you tried to specify both flags?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM