致命错误 - 打开的文件太多

时间:2021-08-23 04:28:58

I try to run PHPUnit Tests in my new machine and I get this error:

我尝试在我的新机器上运行PHPUnit测试,我收到此错误:

PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/usr/lib/php/pear/File/Iterator): failed to open dir: Too many open files' in /usr/lib/php/pear/File/Iterator/Factory.php:114

PHP致命错误:带有消息'RecursiveDirectoryIterator :: __ construct(/ usr / lib / php / pear / File / Iterator)的未捕获异常'UnexpectedValueException':无法打开dir:/ usr / lib / php / pear中的打开文件过多/File/Iterator/Factory.php:114

The same code on the old machine run well...

旧机器上的相同代码运行良好......

New machine environment: PHP Version: PHP 5.3.21 (cli) Older: PHP 5.3.14

新机器环境:PHP版本:PHP 5.3.21(cli)旧版本:PHP 5.3.14

PHPUnit output every time:

每次PHPUnit输出:

................EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE 65 / 66 ( 98%)
E

Time: 34 seconds, Memory: 438.50Mb

There were 50 errors:

1) XXXXXXXXXXX
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/usr/lib/php/pear/File/Iterator): failed to open dir: Too many open files' in /usr/lib/php/pear/File/Iterator/Factory.php:114

4 个解决方案

#1


39  

This can be a limitation on the server where the code is running. Every operating system only allows for a certain number of open files/handles/sockets. This limit is usually further reduced when the server is virtualized. On a Linux server you can check the current limit with ulimit -n, if you have root access you can increase it with the same command. I assume there is a method for Windows server as well. Otherwise there is not much you can do about it (except ask your hoster or administrator to increase it).

这可能是运行代码的服务器的限制。每个操作系统只允许一定数量的打开文件/句柄/套接字。当服务器虚拟化时,通常会进一步减少此限制。在Linux服务器上,您可以使用ulimit -n检查当前限制,如果您具有root访问权限,则可以使用相同的命令增加它。我假设有一个Windows服务器的方法。否则你无能为力(除了要求你的主机或管理员增加它)。

More configurable limitations:

更多可配置的限制:

In /etc/security/limits.conf 
  soft nofile 1024 
  hard nofile 65535 
Increase ulimit by "ulimit -n 65535" 
echo 65535 > /proc/sys/fs/file-max 
In /etc/sysctl.conf 
  fs.file-max=65535 

#2


2  

In php, before the execution, try this

在php中,在执行之前,试试这个

exec('ulimit -S -n 2048');

#3


1  

Don't store DirectoryIterator objects for later; you will get an error saying "too many open files" when you store more than the operating system limit (usually 256 or 1024).

不要存储DirectoryIterator对象以供日后使用;当您存储超过操作系统限制(通常为256或1024)时,您将收到错误消息“太多打开的文件”。

For example, this will yield an error if the directory has too many files:

例如,如果目录包含太多文件,则会产生错误:

<?php 
$files = array(); 
foreach (new DirectoryIterator('myDir') as $file) { 
    $files[] = $file; 
} 
?>

Presumably, this approach is memory intensive as well.

据推测,这种方法也是内存密集型的。

source: http://php.net/manual/pt_BR/directoryiterator.construct.php#87425

来源:http://php.net/manual/pt_BR/directoryiterator.construct.php#87425

#4


0  

How can you up file open limit (Linux or Max OS):

如何提高文件打开限制(Linux或Max OS):

ulimit -n 10000

Solves problem with phpunit or/and phpdbg and Warning: Uncaught ErrorException: require([..file]): failed to open stream: Too many open files in [...]

解决phpunit或/和phpdbg的问题和警告:未捕获错误异常:require([.. file]):无法打开流:[...]中打开的文件过多

#1


39  

This can be a limitation on the server where the code is running. Every operating system only allows for a certain number of open files/handles/sockets. This limit is usually further reduced when the server is virtualized. On a Linux server you can check the current limit with ulimit -n, if you have root access you can increase it with the same command. I assume there is a method for Windows server as well. Otherwise there is not much you can do about it (except ask your hoster or administrator to increase it).

这可能是运行代码的服务器的限制。每个操作系统只允许一定数量的打开文件/句柄/套接字。当服务器虚拟化时,通常会进一步减少此限制。在Linux服务器上,您可以使用ulimit -n检查当前限制,如果您具有root访问权限,则可以使用相同的命令增加它。我假设有一个Windows服务器的方法。否则你无能为力(除了要求你的主机或管理员增加它)。

More configurable limitations:

更多可配置的限制:

In /etc/security/limits.conf 
  soft nofile 1024 
  hard nofile 65535 
Increase ulimit by "ulimit -n 65535" 
echo 65535 > /proc/sys/fs/file-max 
In /etc/sysctl.conf 
  fs.file-max=65535 

#2


2  

In php, before the execution, try this

在php中,在执行之前,试试这个

exec('ulimit -S -n 2048');

#3


1  

Don't store DirectoryIterator objects for later; you will get an error saying "too many open files" when you store more than the operating system limit (usually 256 or 1024).

不要存储DirectoryIterator对象以供日后使用;当您存储超过操作系统限制(通常为256或1024)时,您将收到错误消息“太多打开的文件”。

For example, this will yield an error if the directory has too many files:

例如,如果目录包含太多文件,则会产生错误:

<?php 
$files = array(); 
foreach (new DirectoryIterator('myDir') as $file) { 
    $files[] = $file; 
} 
?>

Presumably, this approach is memory intensive as well.

据推测,这种方法也是内存密集型的。

source: http://php.net/manual/pt_BR/directoryiterator.construct.php#87425

来源:http://php.net/manual/pt_BR/directoryiterator.construct.php#87425

#4


0  

How can you up file open limit (Linux or Max OS):

如何提高文件打开限制(Linux或Max OS):

ulimit -n 10000

Solves problem with phpunit or/and phpdbg and Warning: Uncaught ErrorException: require([..file]): failed to open stream: Too many open files in [...]

解决phpunit或/和phpdbg的问题和警告:未捕获错误异常:require([.. file]):无法打开流:[...]中打开的文件过多