Recursivedirectoryiterator: failed to open dir: Too Many open files

Asked

Viewed 319 times

2

Back and forth, unable to collect data to understand what might be behind this, I get the following error:

Recursivedirectoryiterator::__Construct(path/to/directory) [recursivedirectoryiterator. -Construct]: failed to open dir: Too Many open files

The server where this error arises is running Apache/2.2.24 and PHP/5.3.22.

$workingPath = "caminho/para/directoria";

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($workingPath),
    RecursiveIteratorIterator::SELF_FIRST
);

$files = iterator_to_array($iterator, true); /* tentativa de abafar o erro
                                                como sugerido num tópico do SOEN
                                                mas sem sucesso em resolver a questão */

foreach ($files as $file) {
  // código a executar por cada ficheiro localizado
}

Question

How can I correctly assess the cause of this error and/or find out if there are limits I do not know that may be causing the same?

  • @Bacco I am not indicating anything in that sense... I actually assumed that after each step of the cycle the iterator itself would close the previous file!

  • @Bacco This example is the same code I use in the question, but I am passing a parameter to the iterator. My conversion from iterator to matrix is a step I just applied to see if it solved the question. Regarding foreach() vs while(), I don’t think that’s the problem, but I’ll try...

  • Also try the flag "Filesystemiterator::SKIP_DOTS" in Directoryiterator so it doesn’t enter "recursion recursion": RecursiveDirectoryIterator($workingPath,FilesystemIterator::SKIP_DOTS) (I don’t know if it helps, I’ve never used these iterators)

  • @Bacco I read this topic, but in the place where the iterator is running, it’s a temporary location that has up to 6 files at most :/ So... I don’t know...

  • But what about the tree? You’re using Recursivedirectoryiterator, not Directoryiterator, so it’s going in the subfolders.

  • I left the translation of the answer downstairs, in case it’s useful to someone else. If this is another case, I delete, and/or you can put a correct answer as soon as I find out. If I think of anything else, I’ll let you know. I’ll delete the comments from above, then delete this one.

  • If you can, test with Directoryiterator instead of Recursivedirectoryiterator just to see if the error changes.

  • @Bacco The operation is to go to the main folder and its sub-folders, locating a particular file and thumbnails! At most it is 5 directories so it gives a total of 6 files. But I am running tests...

Show 3 more comments

1 answer

2

This can be a limitation of the server the code is running on. Each OS allows a certain number of/Handles/sockets files. Usually this number is reduced when the server is virtualized. On a Linux server you can check the current limit with ulimit -n, and if you have access root can increase the limit with the same command. Theoretically Windows should have ways to configure this too. Not having these possibilities, there is not much to do but ask the host administrator to increase these limits.

Configurable limits:

  • In /etc/security/Limits.conf

    soft nofile 1024 hard nofile 65535

  • Increase ulimit with "ulimit -n 65535" or

    echo 65535 > /proc/sys/Fs/file-max

  • In /etc/sysctl.conf

    Fs.file-max=65535

Original :https://stackoverflow.com/questions/14748499/fatal-error-too-many-open-files

Browser other questions tagged

You are not signed in. Login or sign up in order to post.