【问题标题】:PHP: How to constantly read a filePHP:如何不断读取文件
【发布时间】:2014-03-21 05:50:05
【问题描述】:

我需要读取一个一直在变化的文件。只有文件,并且永远只有一行会一直变化。

我在这里找到了应该做我想做的事情的以下代码:PHP: How to read a file live that is constantly being written to

但是代码不起作用,页面一直在加载,我尝试像一位用户建议的那样添加“刷新”,但我仍然无法使其工作。

这是代码

$file='/home/user/youfile.txt';
$lastpos = 0;
while (true) {
    usleep(300000); //0.3 s
    clearstatcache(false, $file);
    $len = filesize($file);
    if ($len < $lastpos) {
        //file deleted or reset
        $lastpos = $len;
    }
    elseif ($len > $lastpos) {
        $f = fopen($file, "rb");
        if ($f === false)
            die();
        fseek($f, $lastpos);
        while (!feof($f)) {
            $buffer = fread($f, 4096);
            echo $buffer;
            flush();
        }
        $lastpos = ftell($f);
        fclose($f);
    }
}

请有人看看,让我知道如何解决它。 提前致谢。

【问题讨论】:

  • 你有检查文件路径和权限吗?
  • @LLL 是的文件具有正确的权限 - 。 -rwxrwxrwx 1 个用户 www-data
  • 尝试调试您的代码,看看采用了哪条路线..
  • 抱歉这里有点菜鸟。调试......如何?

标签: php


【解决方案1】:

如果您的文件只有一个字符串,并且您需要在更改时阅读它,请使用以下代码:

$file = '/path/to/test.txt';
$last_modify_time = 0;
while (true) {
    sleep(1); // 1 s
    clearstatcache(true, $file);
    $curr_modify_time = filemtime($file);
    if ($last_modify_time < $curr_modify_time) {
        echo file_get_contents($file);
    }

    $last_modify_time = $curr_modify_time;
}

注意:

filemtime() 以秒为单位返回上次文件修改时间,因此如果您需要每秒检查一次以上的修改,可能需要寻找其他解决方案。

另外,您可能需要添加set_time_limit(0);,这取决于您的要求。

更新:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"
            type="text/javascript">
    </script>
</head>
<body>
    <div id="file_content"></div>
    <script>
        var time = 0;
        setInterval(function() {
            $.ajax({
                type: "POST",
                data: {time : time},
                url: "fileupdate.php",
                success: function (data) {
                    var result = $.parseJSON(data)
                    if (result.content) {
                        $('#file_content').append('<br>' + result.content);
                    }
                    time = result.time;
                }
            });
        }, 1000);
    </script>
</body>

fileupdate.php

<?php
$file = 'test.txt';
$result = array();
clearstatcache(true, $file);

$data['time']    = filemtime($file);
$data['content'] = $_POST['time'] < $data['time']
    ? file_get_contents($file)
    : false;

echo json_encode($data);

【讨论】:

  • 嘿亚历山大。您的解决方案看起来不错,但我的页面仍在继续加载。我在没有“while (true) {”的情况下尝试了您的代码,并且加载的页面显示了 $file 的内容,但显然它没有更新。知道为什么我的页面会挂起这个“while (true) {”吗?
  • @Coenster while(true) 是一个无限循环。该脚本将一直运行,直到发生错误或进程将被终止或 http-server 超时关闭连接。当页面继续加载时 - 这是无限循环脚本的正常行为。如果您需要在没有“页面挂起”的情况下更新页面上的信息,则需要删除 while(true) 循环并创建 javascript ajax-requests。
  • 谢谢。对于 while(true) 不起作用,这有点令人失望。不幸的是,我不知道如何处理 javascript ajax-requests(我什至从未见过 ajax 代码)如果有一些关于 javascript ajax-requests 的教程可以帮助我实现我的目标,或者如果你可以帮助一个示例/演示代码,我将不胜感激。
  • @Coenster 答案已更新。看看通过 ajax 请求更新文件内容的简单示例。将 2 个文件(index.html、fileupdate.php)放在文档根文件夹中。
  • 目前这对我来说很值钱。太棒了!如果你不介意的话,只是最后一个问题。如何编辑此行,以便新内容将替换旧内容而不是将其添加到底部? $('#file_content').append('
    ' + result.content);
【解决方案2】:

您可能会遇到 3 个缺点:

  • 首先,您已有的代码是一个$lastpos。意思是,它将始终在文件末尾查找添加的内容。您在 OP 中不清楚,但我认为这不是您想要的。我认为你的唯一一条线在不断变化,但不一定在改变大小。所以你可能想删除$lastpos = ftell($f);这一行。
  • 其次,同样,您检查文件大小以了解文件是否已更改。但正如我解释的那样,文件可能已经改变,而文件大小保持不变。尝试将文件大小检查更改为检查文件最后编辑日期。
  • 第三,可能也是最重要的:您的 Web 浏览器可能会缓冲您的输出,直到 php 脚本运行完毕,然后才将缓冲的输出释放到浏览器。在 PHP 和 Web 服务器中禁用输出缓冲。 Web 服务器的 gzip/压缩之类的东西也可以强制输出缓冲效果。

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多