mercredi 18 juillet 2018

PHP - too many open files

I developed this function which reads N lines from a file and outputs them.

public function readLinesFromFile($file, $maxLines, $reverse=false)
{
    $lines = file($file);

    if ($lines == false) {
        echo "Datei '$file' konnte nicht geöffnet werden.";
        return false;
    }
    if ($maxLines > count($lines)) {
        echo "\$maxLines ist größer als die Anzahl der Zeilen in der Datei.";
        return false;
    }

    if ($reverse) {
        $lines = array_reverse($lines);
    }

    $tmpArr = array();

    for ($i=0; $i < $maxLines; $i++) {
        array_push($tmpArr, $lines[$i]);
    }

    if ($reverse) {
        $tmpArr = array_reverse($tmpArr);
    }

    $out = "";
    for ($i=0; $i < $maxLines; $i++) {
        $out .= $tmpArr[$i] . "</br>";
    }

    return $out;
}

Everything worked fine, but now of a sudden I get:

Warning: file(C:_Projekte\selenium.env): failed to open stream: Too many open files in C:_Projekte\selenium\vendor\vlucas\phpdotenv\src\Loader.php on line 135

I am using the php method file. It does not return a handle but an array. I am not sure wether the handle is closed by the function or not. I can only see its declaration:

function file(string $filename, int $flags = 0, $context = null): array {}

Is this a PHP Bug or my fault? I know I can increase the limit of open files, but this is not a solution. Should I use another method than file()?


I am fetching the last 100 Lines of a logfile in a specific interval (each 250 ms). I guess the handles are opened but not closed by file()



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire