18.17. Flushing Output to a File
Problem
You want to force all buffered data to be written to a filehandle.
Solution
Use fflush( )
:
fwrite($fh,'There are twelve pumpkins in my house.'); fflush($fh);
This ensures that "There
are
twelve pumpkins in
my
house.” is
written to $fh.
Discussion
To be more efficient, system I/O libraries generally
don’t write something to a file when you tell them
to. Instead, they batch the writes together in a buffer and save all
of them to disk at the same time. Using fflush( )
forces anything pending in the write buffer to be actually written to
disk.
Flushing output can be particularly
helpful when generating an access or activity log. Calling
fflush( ) after each message to log file makes
sure that any person or program monitoring the log file sees the
message as soon as possible.
See Also
Documentation on fflush( ) at
http://www.php.net/fflush.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access