file_get_contents is generating the error: "Too Many open files"

Asked

Viewed 590 times

1

I have a script running on my server through Supervisord. This script queries the database for pending request processing, and when it does, sends the data of each request to an endpoint of a client’s web service.

For each request, I use the file_get_contents to open a file, because I need to send the image data through the base_64_encode.

i log an error in case the processing fails. I have received the following error, referring to file_get_contents

Failed to open stream: Too Many open files

I did not understand very well why this error, since each request is processed one by one. And so, theoretically, the file_get_contents, after reading the contents of the file, closes it right after.

So why does this error occur? How can I fix this?

Note: I am using Ubuntu 14 and, as I researched about the error, it seems to be a configuration on Linux, but I did not understand it very well.

  • 2

    It wouldn’t be because the contents of the file overflowed the buffer?

  • @Guilhermelautert I read here that it is related to a certain ulimit...

  • @Guilhermelautert good idea, instead of opening everything at once with file_get_contents, could be a good use fopen and carry on with the fread... I’ll have to change the logic, maybe

1 answer

2

This may be a limitation on the server the code is running on. The entire operating system only allows a certain number of / identifiers / sockets opened. This limit is usually lower when the server is virtualized. On a server Linux, you can check the current limit with ulimit -n within the server settings, if you have root access, you can increase it with the same command. Otherwise, there isn’t much you can do about it (except ask your hosting administrator to increase it).

If you have access to server root, consider making these changes to confs of Linux, in any case, of course you will have to have a basic notion of Linux to make the changes.

Note that there are two types of limits:

Boundaries soft are simply the limits currently applied.

Boundaries hard mark the maximum value which cannot be exceeded by setting a limit soft

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

Links Úteis;

How to set ulimit values

How do I change the number of open files limit in Linux?

How to Increase Number of Open Files Limit in Linux

Browser other questions tagged

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