Reading a File (With Memory Usage In Mind)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Reading a File (With Memory Usage In Mind)

Post 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?
Last edited by partiallynothing on Sun Oct 30, 2005 4:22 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

file() will return an array of lines in one shot.

fgets() will do what you need though ;)
Post Reply