一个更新文件的活饵料 [英] Live feed of an updating file
问题描述
我有打印的最后50行一个txt文件的PHP文件。但是,此文件被追加每一秒,想查看操作的活饲料。如何才能做到这一点?这是code的php文件:
< PHP
$文件名='/home/duke/aa/servers/df/var/logs.log'; //约500MB
$输出=了shell_exec('EXEC尾-n50'$文件名); //仅打印最后50行
回声str_replace函数(PHP_EOL,'< BR />',$输出); //添加新行
?>
使用AJAX。如果需要跨浏览器兼容,更换AJAX功能,我提供了一个从库如的jQuery 。
< HTML>< HEAD>< /头><身体GT;
< DIV ID =饲料>< / DIV>
<脚本类型=文/ JavaScript的>
变种refreshtime = 10;
功能TC()
{
asyncAjax(GET,myphpfile.php的Math.random(),显示{});
的setTimeout(TC,refreshtime);
}
功能显示(XHR,CDAT)
{
如果(xhr.readyState == 4和&安培; xhr.status == 200)
{
的document.getElementById(饲料)的innerHTML = xhr.responseText。
}
}
功能asyncAjax(方法,URL,QS,回调,callbackData)
{
VAR XMLHTTP =新XMLHtt prequest();
//xmlhttp.cdat=callbackData;
如果(方法==GET)
{
?网址+ = + QS;
}
变种CB =回调;
回调=功能()
{
VAR XHR = XMLHTTP;
//xhr.cdat=callbackData;
VAR cdat2 = callbackData;
CB(XHR,cdat2);
返回;
}
xmlhttp.open(方法,URL,真正的);
xmlhttp.onreadystatechange =回调;
如果(方法==POST){
xmlhttp.setRequestHeader(内容类型,应用程序/ x-WWW的形式urlen codeD');
xmlhttp.send(QS);
}
其他
{
xmlhttp.send(空);
}
}
TC();
< / SCRIPT>
< /身体GT;< / HTML>
您必须创建一个名为myphpfile.php一个PHP文件(或更改code以上,以引用正确的文件),并把下面的它(如取自你的问题):
< PHP
$文件名='/home/duke/aa/servers/df/var/logs.log'; //约500MB
$输出=了shell_exec('EXEC尾-n50'$文件名); //仅打印最后50行
回声str_replace函数(PHP_EOL,'< BR />',$输出); //添加新行
?>
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
?>
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
?>
这篇关于一个更新文件的活饵料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!