Hi
I'm trying to watch a plain text file for any changes.
I want to use PHP and read the file line by line as new lines are added.
Some times 1 line may be added, other times dozens of lines could be added. I want to read each line and check if it matches any keywords..
This is what I've got so far, but it's not line by line... it's buffer size based.
<?php
$file='log.txt';
$line = $lastpos = 0;
while (true) {
usleep(500000);
clearstatcache(false, $file);
$len = filesize($file);
if ($len < $lastpos) {
$lastpos = $len;
}
elseif ($len > $lastpos) {
$f = fopen($file, "rb");
if ($f === false) die();
fseek($f, $lastpos);
while (!feof($f)) {
$buffer = fread($f, 4096);
if ($lastpos > 0) {
if (strpos($buffer, 'MATCH') !== false) echo "$line\r\n";
}
flush();
$line++;
}
$lastpos = ftell($f);
fclose($f);
}
}
?>
Can anyone help point me in the right direction. Ideally I don't want to keep the log in memory as it could grow to a reasonable size.
Thanks