fflush

Forces PHP's buffered output for an open stream to be written out to the operating system.

Signature
fflush(resource $stream): bool
  • Added in PHP 4.0.1

Parameters

ParameterTypeDescription
$streamresourceA valid file pointer opened by fopen() or fsockopen() and not yet closed by fclose().

Return value

booltrue on success or false on failure.

Errors

Throws a TypeError when the argument is not a valid open stream resource.

PHP does not hand every fwrite() straight to the kernel. It accumulates bytes in a per-stream buffer (8 KB by default) and writes when the buffer fills, when you flush, or when the stream closes. fflush() empties that buffer now.

When to reach for it

Any time something outside your script needs to see partial output before the script ends:

  • A progress or heartbeat file another process polls, so a stuck worker is visible.
  • A log you tail while a long import runs.
  • A file another process is about to open and read.
  • Before an operation that might crash, so the diagnostic lines survive.

What it does not do

fflush() pushes bytes from PHP into the OS. The OS may still keep them in its page cache, so a power cut or kernel panic can lose them. When you need the data genuinely on the platter, call fsync() (PHP 8.1+) — it is far slower, which is why it is a separate function.

fclose() already flushes, so a flush immediately before a close is redundant. And if you want unbuffered writes throughout, stream_set_write_buffer($h, 0) is cheaper to reason about than scattering flushes.

It has nothing to do with ob_flush() or flush(), which deal with PHP's output buffer, not a file stream.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Write a heartbeat line from a long-running worker so a supervisor can see it is alive
  • Keep an import log tailable while the import is still running
  • Flush diagnostic output before a risky operation that may kill the process
  • Make a partially written export visible to a process that polls for it

Frequently asked questions

What is the difference between fflush and fsync?

fflush() moves data from PHP's buffer into the operating system, while fsync() (PHP 8.1+) asks the OS to write it to the physical device. Only fsync() protects you against losing data in a crash or power cut.

Do I need to call fflush before fclose?

No. fclose() flushes the buffer as part of closing, so an fflush() on the line before it does nothing useful.

Why is my log file empty while the script is still running?

PHP buffers stream writes, typically in 8 KB blocks, so nothing reaches disk until the buffer fills or the handle is closed. Call fflush() after each entry, or set stream_set_write_buffer($handle, 0).

More examples

A heartbeat another process could watch

Each "tick N\n" is 7 bytes, so the reported size grows 7, 14, 21 as every iteration flushes. Without the fflush() call all three writes would still be buffered and an external reader would see an empty file until the script finished.

Example · php
<?php
$file = "heartbeat.txt";
$h = fopen($file, "w");
stream_set_write_buffer($h, 8192);

for ($i = 1; $i <= 3; $i++) {
    fwrite($h, "tick $i\n");
    fflush($h);
    echo "after tick $i the file holds ", strlen(file_get_contents($file)), " bytes\n";
}

fclose($h);
unlink($file);

Turning buffering off instead

With the write buffer set to zero, every fwrite() reaches the OS immediately and no fflush() is needed — the first read already shows "a\n". This trades throughput for immediacy, so prefer it for low-volume log files and prefer buffering plus periodic flushes for high-volume ones.

Example · php
<?php
$file = "unbuffered.log";
$h = fopen($file, "w");
stream_set_write_buffer($h, 0);      // write straight through

fwrite($h, "a\n");
echo "after first write: ", var_export(file_get_contents($file), true), "\n";

fwrite($h, "b\n");
fclose($h);
echo file_get_contents($file);

unlink($file);

fclose already flushes

The first filesize() reports 0 because the bytes are still buffered; after fclose() the file holds all 24 bytes. The clearstatcache() call is necessary because filesize() cached the earlier zero. This is why an fflush() immediately before an fclose() is redundant.

Example · php
<?php
$file = "closing.log";
$h = fopen($file, "w");
stream_set_write_buffer($h, 8192);

fwrite($h, "never explicitly flushed\n");
var_dump(filesize($file));   // 0 so far

fclose($h);                   // implicit flush happens here
clearstatcache();
var_dump(filesize($file));
echo file_get_contents($file);

unlink($file);

Discussion

  • Be the first to comment on this lesson.