Reading a File (With Memory Usage In Mind)
Posted: Sun Oct 30, 2005 4:09 pm
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:
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:
Any ideas?
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);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
}