php 如何在PHP中观看文件写入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1102229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:06:00  来源:igfitidea点击:

How To watch a file write in PHP?

phptail

提问by freddiefujiwara

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

我想用 PHP 进行诸如 tail 命令之类的运动,但是如何将 watch 附加到文件中?

回答by Emil H

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.

我不相信有什么神奇的方法可以做到这一点。您只需要不断轮询文件大小并输出任何新数据。这实际上很容易,唯一需要注意的是文件大小和其他统计数据缓存在 php 中。解决这个问题的方法是clearstatcache()在输出任何数据之前调用。

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");

回答by Glenn Plas

$handle = popen("tail -f /var/log/your_file.log 2>&1", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "$buffer\n";
    flush();
}
pclose($handle);

回答by Ben

Checkout php-tail on Google code. It's a 2 file implementation with PHP and Javascript and it has very little overhead in my testing.

在 Google 代码上签出php-tail。这是一个使用 PHP 和 Javascript 的 2 文件实现,在我的测试中它的开销很小。

It even supports filtering with a grep keyword (useful for ffmpeg which spits out frame rate etc every second).

它甚至支持使用 grep 关键字进行过滤(对于每秒输出帧速率等的 ffmpeg 很有用)。

回答by Artem Barger

$handler = fopen('somefile.txt', 'r');

// move you at the end of file
fseek($handler, filesize( ));
// move you at the begining of file
fseek($handler, 0);

And probably you will want to consider a use of stream_get_line

并且您可能会考虑使用stream_get_line

回答by TheHippo

Instead of polling filesize you regular checking the file modification time: filemtime

不是轮询文件大小,而是定期检查文件修改时间:filemtime

回答by Anthonus

Below is what I adapted from above. Call it periodically with an ajax call and append to your 'holder' (textarea)... Hope this helps... thank you to all of you who contribute to stackoverflow and other such forums!

下面是我从上面改编的。使用 ajax 调用定期调用它并附加到您的“持有人”(textarea)...希望这会有所帮助...感谢所有为 stackoverflow 和其他此类论坛做出贡献的人!

/* Used by the programming module to output debug.txt */
session_start();
$_SESSION['tailSize'] = filesize("./debugLog.txt");
if($_SESSION['tailPrevSize'] == '' || $_SESSION['tailPrevSize'] > $_SESSION['tailSize'])
{
      $_SESSION['tailPrevSize'] = $_SESSION['tailSize'];
}
$tailDiff = $_SESSION['tailSize'] - $_SESSION['tailPrevSize'];
$_SESSION['tailPrevSize'] = $_SESSION['tailSize'];

/* Include your own security checks (valid user, etc) if required here */

if(!$valid_user) {
   echo "Invalid system mode for this page.";
}
$handle = popen("tail -c ".$tailDiff." ./debugLog.txt 2>&1", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo "$buffer";
    flush(); 
}
pclose($handle);