Page 1 of 1

Reading a File (With Memory Usage In Mind)

Posted: Sun Oct 30, 2005 4:09 pm
by partiallynothing
I need to parse a very large system log file, where each line contains information which needs to be parsed.

Currently my code looks something like this:

Code: Select all

// Open handle to log file, read into variable, and close handle
$h = fopen($log_file, 'r');
$contents = fread($h, filesize($log_file));
fclose($h);

$file_lines = explode("\n", $contents);
Using that I would then do a foreach through the array and parse the line.
The obviouse problem is that if the file is very large the memory consumption of the script will sky-rocket.

My question; does anyone know of a way to read a file in line by line, so I could do something like this:

Code: Select all

while(read_line($file_handle)) {
  // parse line
}
Any ideas?

Posted: Sun Oct 30, 2005 4:14 pm
by Chris Corbyn
file() will return an array of lines in one shot.

fgets() will do what you need though ;)