请教PHP fflush 的问题, not working

WBOY
풀어 주다: 2016-06-06 20:21:44
원래의
1370명이 탐색했습니다.

有没有用过 fflush 函数

官方解释是这样说的:
Flushes the output to a file
This function forces a write of all buffered output to the resource pointed to by the file handle.

也就是说把文件的写入缓存强行释放输出到文件指针.
http://php.net/manual/en/function.fflush.php

但我发现 fwrite 在执行完之后, 内容就马上写入文件了. 不需要fflush, 也不需要fclose, 对这个问题感到很疑惑, fflush 究竟有提供了什么作用.

代码:

<code class="php"><?php $fp = fopen("fflush.txt", "w+");
for ($i = 0; $i < 10; $i++) {
    fwrite($fp, "Write something here\n"); // 马上写入内容, 根本不存在 buffer 一说啊!
    sleep(1);
}
fflush($fp);
fclose($fp);</code></code>
로그인 후 복사
로그인 후 복사

回复内容:

有没有用过 fflush 函数

官方解释是这样说的:
Flushes the output to a file
This function forces a write of all buffered output to the resource pointed to by the file handle.

也就是说把文件的写入缓存强行释放输出到文件指针.
http://php.net/manual/en/function.fflush.php

但我发现 fwrite 在执行完之后, 内容就马上写入文件了. 不需要fflush, 也不需要fclose, 对这个问题感到很疑惑, fflush 究竟有提供了什么作用.

代码:

<code class="php"><?php $fp = fopen("fflush.txt", "w+");
for ($i = 0; $i < 10; $i++) {
    fwrite($fp, "Write something here\n"); // 马上写入内容, 根本不存在 buffer 一说啊!
    sleep(1);
}
fflush($fp);
fclose($fp);</code></code>
로그인 후 복사
로그인 후 복사

给你参考一个方法 stream_set_write_buffer ,默认的 buffer_size8K ,也就是说,fwrite 如果一次写入的数据量大于 buffer_size ,会拆分写入。这种情况在多进程操作同一文件时需要考虑,通常情况下 fflush 是没多大作用的,不过加了也无所谓,毕竟 fopen 还能打开其他的 文件 似的资源。

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!