PHP的fflush()函数的解释 [英] Explanation of PHP's fflush() function

查看:144
本文介绍了PHP的fflush()函数的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

手册说,它将输出刷新到一个文件。再加上他们举了一个例子。

  $ filename ='bar.txt'; 

$ file = fopen($ filename,'r +');
倒带($文件);
fwrite($ file,'Foo');
fflush($ file);
ftruncate($ file,ftell($ file));
fclose($ file);

我试图了解它的必要性。我做了以下事情:


  • 我创建了一个bar.txt文件

  • 脚本

  • 我打开bar.txt,在里面看到'Foo',然后我再次将文件置空。然后,我从脚本中删除了 fflush($ file); ,然后再次运行。

  • 结果完全一样。我又把这个文件空了。

  • 之后,我将 frwite()行更改为 fwrite($ file,'Foo'); die();

  • 同样的结果...里面有一个'Foo'的文件。



现在,我没有看到使用 fflush()



在这个例子中我看不到fflush的必要性。你能提供一个例子,其中 fflush()确实是必要的。

链接:
http://php.net/manual/en/function.fflush.php

解决方案

fflush() flush(),不同之处在于输出是发送到文件而不是浏览器。



基本上它所做的是强制数据立即写入,而不是将它们全部存储在缓冲区中,直到准备写入。这可以在几种情况下使用,例如 - 使用linux命令 tail 监视日志: https://en.wikipedia.org/wiki/Tail_(Unix)


The manual says that it flushes the output to a file. Plus they give an example.

$filename = 'bar.txt';

$file = fopen($filename, 'r+');
rewind($file);
fwrite($file, 'Foo');
fflush($file);
ftruncate($file, ftell($file));
fclose($file);

I tried to understand its necessity. What I did was the following:

  • I created a bar.txt file
  • I runned the script
  • I opened bar.txt and saw 'Foo' inside it, then I made the file empty again.
  • Then... I removed fflush($file); from the script and runned it again.
  • The result was exactly the same. I made the file empty yet another time.
  • After that I changed the frwite() line to fwrite($file, 'Foo'); die();
  • Again... same results... a file with 'Foo' inside it.

Now, I don't see the point of using fflush().

I can't see the necessity of fflush in this example. Can you provide an example in which fflush() is really necessary.

Link: http://php.net/manual/en/function.fflush.php

解决方案

fflush() provides the same necessity as flush(), the difference being that the output is sent to a file rather than a browser.

Basically what it's doing is forcing data to be written immediately rather than storing it all in a buffer until it is ready to be written. This can be useful in several situations, for example - monitoring logs using the linux command tail: https://en.wikipedia.org/wiki/Tail_(Unix).

这篇关于PHP的fflush()函数的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆