阻止 PHP 中的文件读取?

发布于 2024-07-30 07:22:26 字数 95 浏览 3 评论 0原文

我想从文本文件中读取所有内容并回显它。 但是在我阅读时可能会有更多行写入文本文件,因此我不希望脚本在到达文件末尾时退出,而是希望它永远等待更多行。 这在 PHP 中可能吗?

I want to read everything from a textfile and echo it. But there might be more lines written to the text-file while I'm reading so I don't want the script to exit when it has reached the end of the file, instead I wan't it to wait forever for more lines. Is this possible in php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦回梦里 2024-08-06 07:22:26

这只是一个猜测,但尝试传递(passthru)“tail -f”输出。

但你需要找到一种方法来刷新()你的缓冲区。


恕我直言,更好的解决方案是构建一个 ajax 站点。

将文件内容读入数组。 存储会话中的行数。 打印文件的内容。

每 x 秒向检查文件的脚本发起一个 ajax 请求,如果行数大于会话数,则将结果附加到页面。


你可以使用 popen() inststed:

$f = popen("tail -f /where/ever/your/file/is 2>&1", 'r');
while(!feof($f)) {
    $buffer = fgets($f);
    echo "$buffer\n";
    flush();
    sleep(1);
}
pclose($f)

睡眠很重要,没有它你将拥有 100% 的 CPU 时间。

this is just a guess, but try to pass through (passthru) a "tail -f" output.

but you will need to find a way to flush() your buffer.


IMHO a much nicer solution would be to build a ajax site.

read the contents of the file in to an array. store the number of lines in the session. print the content of the file.

start an ajax request every x seconds to a script which checks the file, if the line count is greater then the session count append the result to the page.


you could use popen() inststed:

$f = popen("tail -f /where/ever/your/file/is 2>&1", 'r');
while(!feof($f)) {
    $buffer = fgets($f);
    echo "$buffer\n";
    flush();
    sleep(1);
}
pclose($f)

the sleep is important, without it you will have 100% CPU time.

情深已缘浅 2024-08-06 07:22:26

事实上,当您“回显”它时,它会进入缓冲区。 因此,您想要的是“附加”新内容(如果在浏览器仍在接收输出时添加新内容)。 这是不可能的(但有一些方法可以实现这一点)。

In fact, when you "echo" it, it goes to the buffer. So what you want is "appending" the new content if it's added while the browser is still receiving output. And this is not possible (but there are some approaches to this).

肤浅与狂妄 2024-08-06 07:22:26

我解决了。

诀窍是使用 fopen,当到达 eof 时,将光标移动到上一个位置并从那里继续读取。

<?php
$handle = fopen('text.txt', 'r');
$lastpos = 0;
while(true){
   if (!feof($handle)){
       echo fread($handle,8192);
       flush();
       $lastpos = ftell($handle);
   }else{
       fseek($handle,$lastpos);
   }
}
?>

cpu消耗还是很大,不知道怎么解决。

I solved it.

The trick was to use fopen and when eof is reached move the cursor to the previous position and continue reading from there.

<?php
$handle = fopen('text.txt', 'r');
$lastpos = 0;
while(true){
   if (!feof($handle)){
       echo fread($handle,8192);
       flush();
       $lastpos = ftell($handle);
   }else{
       fseek($handle,$lastpos);
   }
}
?>

Still consumes pretty much cpu though, don't know how to solve that.

不知所踪 2024-08-06 07:22:26

您还可以使用 filemtime:获取最新修改时间戳,发送输出,最后再次将存储的 filemtime 与当前时间进行比较。

无论如何,如果您希望脚本与浏览器(或客户端)同时运行,您应该使用块(freadflush)发送输出,然后检查任何改变在最后。 如果有任何变化,重新打开文件并从最新位置读取(可以在 while(!feof()) 循环之外获取位置)。

You may also use filemtime: you get latest modification timestamp, send the output and at the end compare again the stored filemtime with the current one.

Anyway, if you want the script go at the same time that the browser (or client), you should send the output using chunks (fread, flush), then check any changes at the end. If there are any changes, re-open the file and read from the latest position (you can get the position outside of the loop of while(!feof())).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文