Recipe 23.13. Flushing Output to a File

23.13.1. Problem

You want to force all buffered data to be written to a filehandle.

23.13.2. Solution

Use fflush( ), as in Example 23-35.

Flushing output

<?php fwrite($fh,'There are twelve pumpkins in my house.'); fflush($fh); ?>

Example 23-35 ensures that "There are twelve pumpkins in my house." is written to $fh.

23.13.3. 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 logfile makes sure that any person or program monitoring the logfile sees the message as soon as possible.

23.13.4. See Also

Documentation on fflush( ) at

PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: