Example 1 — Flush Before Reading
Write text to a file, flush it and read the file back while the handle is still open.
<?php
$path = sys_get_temp_dir() . '/codetofun_fflush.txt';
$handle = fopen($path, 'w');
fwrite($handle, 'Hello');
fflush($handle);
echo 'File now contains: ', file_get_contents($path), PHP_EOL;
fclose($handle);
unlink($path); How It Works
- fopen() opens the temporary file in write mode.
- fwrite() adds the word Hello to PHP's write buffer.
- fflush() pushes the buffered data into the file right away.
- file_get_contents() can now read Hello, even though the handle is still open.

