RecursiveDirectoryIterator throws UnexpectedValueException on quot;Too many open filesquot;(RecursiveDirectoryIterator 在“打开的文件太多上抛出 UnexpectedValueException)
问题描述
以下代码:
$zip = new ZipArchive();
if ($zip->open('./archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./folder/"));
foreach ($iterator as $key => $value) {
try {
$zip->addFile(realpath($key), $key);
echo "'$key' successfully added.
";
} catch (Exception $e) {
echo "ERROR: Could not add the file '$key': $e
";
}
}
$zip->close();
如果您尝试迭代的子文件夹中有太多文件,则会引发以下异常:
Throws the following exception if there are too many files in a sub-folder which you're trying to iterate over:
Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(./some/path/): failed to open dir: Too many open files' in /some/other/path/zip.php:24
Stack trace:
#0 [internal function]: RecursiveDirectoryIterator->__construct('./some/path/')
#1 /some/other/path/zip.php(24): RecursiveDirectoryIterator->getChildren()
#2 {main}
thrown in /some/other/path/zip.php on line 24
如何在不遇到此异常的情况下成功迭代大量文件夹和文件?
How can you successfully iterate over a large amount of folders and files without experiencing this exception?
推荐答案
只需将迭代器转换为数组,iterator_to_array
函数,看来您可以迭代任意数量的文件:
Simply by converting the iterator to an array with the iterator_to_array
function, it seems like you can iterate over as many files as you'd like:
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("./folder/"));
$files = iterator_to_array($iterator, true);
// iterate over the directory
// add each file found to the archive
foreach ($files as $key => $value) {
try {
$zip->addFile(realpath($key), $key);
echo "'$key' successfully added.
";
} catch (Exception $e) {
echo "ERROR: Could not add the file '$key': $e
";
}
}
这篇关于RecursiveDirectoryIterator 在“打开的文件太多"上抛出 UnexpectedValueException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:RecursiveDirectoryIterator 在“打开的文件太多"上抛出 UnexpectedValueException


基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01