Implementation of “tail -f” in PHP
This is a small algorithm to implement a functionality similar to “tail -f” in PHP. The script is able watch a file in real time and do something everytime a new line is added (for example, a log file). It doesn’t implement the “tail” functionnality however (outputing only the end of the file) and instead starts processing the file from the beginning.
$file = @ fopen($filename, 'r');
$pos = 0;
while (true) {
fseek($file, $pos);
while ($line = fgets($file)) {
// do something with $line
}
$pos = ftell($file);
sleep(1);
}
fclose($file);