Efficiently counting the number of lines of a text file. (200mb+)
This will use less memory, since it doesn’t load the whole file into memory: $file=”largefile.txt”; $linecount = 0; $handle = fopen($file, “r”); while(!feof($handle)){ $line = fgets($handle); $linecount++; } fclose($handle); echo $linecount; fgets loads a single line into memory (if the second argument $length is omitted it will keep reading from the stream until it reaches … Read more