PHP fflush()函数用法介绍

PHP中的fflush()函数是一个内置函数, 用于将所有缓冲的输出写入打开的文件。 fflush()函数强制将所有缓冲的输出写入文件句柄指向的资源。 fflush()函数返回true成功与false失败。
语法如下:

fflush($file)

参数:PHP中的fflush()函数仅接受一个参数$ file。它指定打开的文件流。
返回值:成功返回TRUE, 失败返回FALSE。
错误与异常:
  1. 如果文件指针无效, 则fflush()函数将导致错误。
  2. 指向的文件必须由fopen()或fsockopen()打开, 并由fclose()关闭。
下面的程序说明了fflush()函数。
程序1:在以下程序中, 文件名为singleline.txt包含一行信息, 即"此文件由一行组成。"。
< ?php// The file is opened using fopen() function $check = fopen ( "singleline.txt" , "r" ); $seq = fgets ( $check ); // Writing buffered output to a file // until the end-of-file is reached while (! feof ( $check )) fflush ( $check ); // The file is closed using fclose() function fclose( $check ); ?>

输出如下:
This file consists of a single line.

程序2:在以下程序中, 文件名为gfg.txt包含以下文本。
这是第一行。这是第二行。这是第三行。
< ?php// The file is opened using fopen() function $check = fopen ( "gfg.txt" , "r" ); $seq = fgets ( $check ); // Writing buffered output to a file // until the end-of-file is reached while (! feof ( $check )) fflush ( $check ); // The file is closed using fclose() function fclose( $check ); ?>

输出如下:
This is the first line.This is the second line.This is the third line.

参考:
【PHP fflush()函数用法介绍】http://php.net/manual/en/function.fflush.php

    推荐阅读