Jump to content

[SOLVED] Parse big log file


SycoSyco

Recommended Posts

I need to parse a log file for a virus scan program (on the server) to see if the last few entries contain the message regarding finding and removing a file just uploaded. Then I can print a message to the screen saying the file was deleted because a virus was found.

 

The log file will grow to 10Mb so I don't want to write the whole thing into memory every time using file() but cant think of a neat way to read just the last few lines.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/
Share on other sites

on IIS (blah) so cant tac or tail.

 

"split up the file into multiple flat files"

 

Using PHP? How could I achieve this without using tons of memory? I would have to split into chunks then take the last chuck, check its big enough, then parse it. would this be more efficient then using file()?

 

 

I think using fopen() and then fseek()ing toward the end of the file will be easy on resources.  It shouldn't read the whole thing into memory.

 

Something like this...

<?php

$offset_from_end = 800;  // However much you want from the end.
$filesize = filesize($filename);
if ($filesize < $offset_from_end) $offset = 0;
else $offset = $filesize - $offset_from_end;

$fh = fopen($filename,'r');
fseek($fh,$offset);
$contents = fread($fh,$offset_from_end);
fclose($fh);

// Do stuff with $contents....

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.