RecursiveDirectoryIterator在“Too many open files”上抛出UnexpectedValueException。 [英] RecursiveDirectoryIterator throws UnexpectedValueException on "Too many open files"
本文介绍了RecursiveDirectoryIterator在“Too many open files”上抛出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.\n";
} catch (Exception $e) {
echo "ERROR: Could not add the file '$key': $e\n";
}
}
$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.\n";
} catch (Exception $e) {
echo "ERROR: Could not add the file '$key': $e\n";
}
}
这篇关于RecursiveDirectoryIterator在“Too many open files”上抛出UnexpectedValueException。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文