How to Tail a File with PHP

Advertisement

Advertisement

This code will allow you to tail a file in PHP. It is cheating a little bit though because it is actually making a system call to tail, so you have to have tail installed. It is meant for Linux machines not Windows.

<?php
$handle = popen("tail -f /etc/httpd/logs/access.log 2>&1", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "$buffer<br/>\n";
    ob_flush();
    flush();
}
pclose($handle);

Advertisement

Advertisement