Jump to content

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....

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.