Live feed of an updating file

10,196

With ajax. If you need cross-browser compatibility, replace the AJAX function I provided with one from a library like jQuery.

<html><head></head><body>
<div id="feed"></div>
<script type="text/javascript">
var refreshtime=10;
function tc()
{
asyncAjax("GET","myphpfile.php",Math.random(),display,{});
setTimeout(tc,refreshtime);
}
function display(xhr,cdat)
{
 if(xhr.readyState==4 && xhr.status==200)
 {
   document.getElementById("feed").innerHTML=xhr.responseText;
 }
}
function asyncAjax(method,url,qs,callback,callbackData)
{
    var xmlhttp=new XMLHttpRequest();
    //xmlhttp.cdat=callbackData;
    if(method=="GET")
    {
        url+="?"+qs;
    }
    var cb=callback;
    callback=function()
    {
        var xhr=xmlhttp;
        //xhr.cdat=callbackData;
        var cdat2=callbackData;
        cb(xhr,cdat2);
        return;
    }
    xmlhttp.open(method,url,true);
    xmlhttp.onreadystatechange=callback;
    if(method=="POST"){
            xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlhttp.send(qs);
    }
    else
    {
            xmlhttp.send(null);
    }
}
tc();
</script>
</body></html>

You will have to create a php file called myphpfile.php (or change the code above to reference the correct file) and put the following in it (as taken from your question):

<?php
$filename = '/home/duke/aa/servers/df/var/logs.log';  //about 500MB
$output = shell_exec('exec tail -n50 ' . $filename);  //only print last 50 lines
echo str_replace(PHP_EOL, '<br />', $output);         //add newlines
?>
Share:
10,196
dukevin
Author by

dukevin

.-"-.__ | .' / ,-| ```'-, || . ` / /@)|_ `-, | / ( ~ \_```""--.,_', : _.' \ /```''''""`^ / | / `| : / \/ | | . / __/ | |. | | __/ / | | | "-._ | _/ _/=_// || : '. `~~`~^| /=/-_/_/`~~`~~~`~~`~^~`. `&gt; ' . \ ~/_/_ /" ` . ` ' . | .' ,'~^~`^| |~^`^~~`~~~^~~~^~`; ' .-' | | | \ ` : ` | :| | : | |: | || ' | |/ | | : |_/ | . '

Updated on June 08, 2022

Comments

  • dukevin
    dukevin almost 2 years

    I have a php file that prints the last 50 lines of a txt file. However, this file gets appended every second and would like to view a "live feed" of the action. How can this be done? This is the code for the php file:

    <?php
    $filename = '/home/duke/aa/servers/df/var/logs.log';  //about 500MB
    $output = shell_exec('exec tail -n50 ' . $filename);  //only print last 50 lines
    echo str_replace(PHP_EOL, '<br />', $output);         //add newlines
    ?>
    
  • dukevin
    dukevin over 12 years
    Was looking for an answer for a webrowser to point to this file and view the file in real time.
  • dukevin
    dukevin over 12 years
    This looked promising, however I get a blank screen with this code
  • someone
    someone over 12 years
    Did you replace myphpfile.php with the location of the file containing the code provided in your question?
  • dukevin
    dukevin over 12 years
    Yes, I have. I have 2 files now, one with my short code above, and your code. Do I point my browser to your code or my code with a header like <script src="js.js">?
  • dukevin
    dukevin over 12 years
    Hm, no luck. 209.141.56.244/test/query.html is where your code is, and 209.141.56.244/test/logtee.php is my file
  • someone
    someone over 12 years
    Oh, I see the problem: you're missing a </div> after the <div id="feed">, and a </script> at the end. Try recopying my code again, I made a few changes.
  • someone
    someone over 12 years
    Now you just have to change the filename again (myphpfile.php to logtee.php) and I think it'll work-- it's showing ton of requests to the non-existent myphpfile.php in my test.
  • dukevin
    dukevin over 12 years
    Nevermind, it worked! Thanks very much. A million gold stars for you :)
  • jeroen
    jeroen over 12 years
    +1 Although using ajax and a timeout function is the way to go, the php is going to be a lot more complicated to show just the new lines instead of the 50 last lines (to avoid doubles and / or missing entries).