PHP ZipArchive 未添加超过 700 个文件
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是因为操作系统中打开文件的最大数量(请参阅 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).
该问题如下所述: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?
我通过增加 ulimit 解决了这个问题:
ulimit -n 8192
。I solved this problem by increasing the ulimit:
ulimit -n 8192
.