RecursiveDirectoryIterator: failed to open dir: Too many open files

2

In a matter of minutes, and without being able to gather data to understand what might be behind it, 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 occurs 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 evaluate the cause of this error and / or find out if there are any limits that I do not know could be causing the same?

    
asked by anonymous 04.04.2014 / 18:29

1 answer

2

This can be a limitation of the server where the code is running. Each OS allows a certain number of files / handles / sockets. 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 you can increase the limit with the same command. Theoretically Windows must have the means to configure this as well. 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: link

    
04.04.2014 / 19:01