0
votes

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();
3
Any error messages when turning on error_reporting? Does the class have error logging? - Pekka
And you are sure you are not simply running out of time or memory? - Pekka
(Deleted previous comment.) I'm rather certain that I'm not running out of memory or time (max_execution_time is set 0, and the output is correctly displayed after the zip file has been created). However, I have discovered that I'm getting an error message. Please see my edit. - ShdNx
Just a thought: maybe you should try adding 701+ small plaintext files or something similar, to make sure it's an issue with the number of files rather than some other weird coincidence. - Joonas Trussmann
Can you show the full code you're using? - Pekka

3 Answers

1
votes

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

0
votes
-1
votes

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