如何观看用PHP写的文件?

编程入门 行业动态 更新时间:2024-10-27 16:28:55
本文介绍了如何观看用PHP写的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用PHP进行诸如tail命令之类的动作, 但是如何观看附加到文件的内容?

I want to make movement such as the tail command with PHP, but how may watch append to the file?

推荐答案

我不认为有某种神奇的方法可以做到这一点.您只需要连续轮询文件大小并输出任何新数据即可.这实际上是很容易的,需要注意的唯一真实的事情是文件大小和其他统计数据都存储在php中.解决方案是在输出任何数据之前调用clearstatcache().

I don't believe that there's some magical way to do it. You just have to continuously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

这是一个快速示例,其中不包含任何错误处理:

Here's a quick sample, that doesn't include any error handling:

function follow($file) { $size = 0; while (true) { clearstatcache(); $currentSize = filesize($file); if ($size == $currentSize) { usleep(100); continue; } $fh = fopen($file, "r"); fseek($fh, $size); while ($d = fgets($fh)) { echo $d; } fclose($fh); $size = $currentSize; } } follow("file.txt");

更多推荐

如何观看用PHP写的文件?

本文发布于:2023-11-06 17:36:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!