SycoSyco Posted June 22, 2007 Share Posted June 22, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/ Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 split up the file into multiple flat files then recall what you need via functions Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/#findComment-280369 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 On *nix? Use 'tail' Through PHP? Use fopen and fseek. Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/#findComment-280371 Share on other sites More sharing options...
SycoSyco Posted June 22, 2007 Author Share Posted June 22, 2007 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()? Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/#findComment-280383 Share on other sites More sharing options...
Wildbug Posted June 22, 2007 Share Posted June 22, 2007 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.... ?> Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/#findComment-280389 Share on other sites More sharing options...
SycoSyco Posted June 22, 2007 Author Share Posted June 22, 2007 nice one wildbug fseek() looks just the ticket! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/56770-solved-parse-big-log-file/#findComment-280436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.