Tuesday, 7 June 2016

fflush

18.18. Flushing Output to a File

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

18.18.2. 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.

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

18.18.4. See Also
Documentation on fflush( ) at http://www.php.net/fflush.

Friday, 3 June 2016

Questions: File functions

What is the output of following code?
<?php
$name = 'fpassthru.txt';

$str = <<<EOF
FOO
BAR
BAZ
EOF;

$fp = fopen($name, 'w');
fwrite($fp, $str);
fclose($fp);

$fp = fopen($name, 'r');
fgets($fp);
fpassthru($fp);
fclose($fp);
?>

a) FOO BAR
b) BAR BAZ
c) FOO BAZ
d) BAR FOO