首页 / PHP / php – 更新文件的实时订阅源
php – 更新文件的实时订阅源
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 更新文件的实时订阅源,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1641字,纯文字阅读大概需要3分钟。
内容图文
![php – 更新文件的实时订阅源](/upload/InfoBanner/zyjiaocheng/720/a4cba8535e544712bdbc57b001888a5d.jpg)
我有一个php文件打印txt文件的最后50行.但是,此文件每秒都会被追加,并且希望查看该操作的“实时提要”.如何才能做到这一点?这是php文件的代码:
<?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
?>
解决方法:
用ajax.如果您需要跨浏览器兼容性,请将我提供的AJAX功能替换为jQuery等库中的AJAX功能.
<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>
您将不得不创建一个名为myphpfile.php的php文件(或更改上面的代码以引用正确的文件)并在其中添加以下内容(从您的问题中获取):
<?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
?>
内容总结
以上是互联网集市为您收集整理的php – 更新文件的实时订阅源全部内容,希望文章能够帮你解决php – 更新文件的实时订阅源所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。