PHP Warning: Too many open files - any ideas?

I am running PHP 5.2 on Fedora, and I continue to receive this warning after about 1000 iterations of my loop, which means the program has stopped working and needs to be restarted. I could set this to exit after 1000 iterations and restart via cron shortly afterwards, but this seems like a cowardly exit. The cycle follows; I must add that get_load()transforms the challenge file_get_contents().

while ($row = select_row($sql))
{
    while (($load = get_load()) > 10)
    {
        echo "Going to sleep (load: ".$load.")\n";
        sleep(60*3);
    }
    $id = $row['id'];
    foreach ($sizes as $abbr=>$size)
    {
        if($row[$size] != "yes")
        {
            continue;
        }
        $filename = "/images/".$abbr."/".$id.".jpg";
        $tmp_file = "/tmp/".$id.".jpg";
        if ($size == "large")
        {
            //We want to progressively interlace our large bookcovers because it saves on filesave above 10K.
            $cmd = "convert -strip -interlace Plane ".$filename." ".$tmp_file;
        }
        else
        {
            $cmd = "convert -strip ".$filename." ".$tmp_file;
        }
        $convert = popen($cmd." 2>&1", "r");
        if (is_resource($convert))
        {
            echo fgets($convert);
            if(pclose($convert) == 0)
            {
                 //Upload converted file to remote server
            }
            unlink($tmp_file);
        }
    }

Edit: after reading the first two answers, I realized that, taking out the file upload code, which is not related to my problem, I took out my operator pclose(). Paste pclose()as shown in my code.

Further editing: Added get_load()on request

function get_load()
{
    $load = explode(" ", file_get_contents("/proc/loadavg"));
    return $load[0];
}
+3
5
+4

, pclose().

+2

popen : . , FALSE is_resource? , - , , , , . , - , , .

0
source

I know what you said you were get_load()using file_get_contents(), but to make sure ... does it close all the files it opens correctly? Could you send the code from this function?

Edit: despite the built-in function, try using something else other than file_get_contents()to read the file and see what happens.

0
source

Source: https://habr.com/ru/post/1703830/


All Articles