I am writing a tail function in PHP, and using jQuery to refresh a div area with any content from a log. I am using a session in PHP to hold a file pointer so when the function in PHP is called again, from the setInterval (jQuery), it will know where it left off and print anything that is new to the end of the file. For some reason since I began to use a session i cant see it stream to browser until the task is finished. Please help me with this. Thanks!
Here is my jQuery:
setInterval(function() {
$.get("ajax.php?function=tail", function(data) {
$("#tail").append(data);
}, 'html');
}, 2000);
Here is my PHP:
function tail() {
$file = "/path/to/the/log/file.log";
$handle = fopen($file, "r");
if(isset($_SESSION['ftell'])) {
clearstatcache();
fseek($handle, $_SESSION['ftell']);
while ($buffer = fgets($handle)) {
echo $buffer . "<br />";
}
fclose($handle);
$_SESSION['ftell'] = ftell($handle);
} else {
fseek($handle, -1024, SEEK_END);
$_SESSION['ftell'] = ftell($handle);
}
}
try to flash the buffer after the echo with http://php.net/manual/en/function.ob-flush.php