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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14748499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:54:40  来源:igfitidea点击:

Fatal Error - Too many open files

phpphpunit

提问by Mauro

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 致命错误:未捕获的异常 'UnexpectedValueException' 带有消息 'RecursiveDirectoryIterator::__construct(/usr/lib/php/pear/File/Iterator): 无法打开目录:/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

回答by Gerald Schneider

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 

回答by pablorsk

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

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

ulimit -n 10000

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

解决phpunitor/and phpdbgand 的问题Warning: Uncaught ErrorException: require([..file]): failed to open stream: Too many open files in [...]

回答by R?mulo Z. C. Cunha

In php, before the execution, try this

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

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

回答by user1666651

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

回答by Lando

After 'waking' my computer from sleep mode I ran into this problem.

从睡眠模式“唤醒”我的计算机后,我遇到了这个问题。

Restarting php-fpm like so fixed it. Classic turn it off & back on again solution.

像这样重新启动 php-fpm 修复了它。经典关闭并重新打开解决方案。

sudo /etc/init.d/php-fpm restart

sudo /etc/init.d/php-fpm restart

I think this may be related to xdebug which I recently added to php.

我认为这可能与我最近添加到 php 的 xdebug 有关。

回答by Nguy?n Hoàng Tu?n

Maybe, you have some error with file /etc/init.d/phpx.x-fpm. Let's restart it:

也许,您对 file 有一些错误/etc/init.d/phpx.x-fpm。让我们重新启动它:

sudo /etc/init.d/php7.2-fpm restart

回答by Hyman Kinsella

I've noticed this occur in PHP when you forget to wrap something in a closure. Carefully look at your recent diffs and you might be able to get to the bottom of this (in my case, I referenced $fakerin a Laravel PHP unit factory without having a closure.

我注意到当您忘记在闭包中包装某些东西时,会在 PHP 中发生这种情况。仔细查看您最近的差异,您可能能够$faker深入了解这一点(在我的情况下,我在 Laravel PHP 单元工厂中引用了没有闭包的内容。

回答by Kamil D?browski

on server debian you can go to also to

在服务器 debian 上你也可以去

/etc/php/php7.xx/fpm/pool.d/www.conf

/etc/php/php7.xx/fpm/pool.d/www.conf

rlimit_files = 10000

rlimit_files = 10000

/etc/init.d/php7.xx restart