W3docs

fflush()

We understand that you want an article that can outrank the current W3Schools article on the PHP fflush() function. We'll create a comprehensive and detailed

Introduction to PHP fflush() Function

The fflush() function in PHP forces any data still sitting in a file pointer's write buffer to be written to the underlying stream immediately, instead of waiting for the buffer to fill or for the stream to be closed.

When you call fwrite(), PHP does not necessarily push the bytes straight to disk. For performance, the data is collected in an in-memory buffer and written out in larger chunks. That is usually what you want — but sometimes you need the data to land now: a tail-able log file, a long-running worker that another process is reading, or a script that may be killed before it finishes. fflush() is the function that says "don't wait, write what you have."

This page covers the syntax, parameters, return value, and several runnable examples, plus the gotchas that trip people up — including the crucial difference between fflush() and OS-level disk syncing.

Syntax

The syntax of the fflush() function is as follows:

The PHP syntax of fflush()

bool fflush ( resource $stream )
  • stream: the file pointer to flush

Parameters

The fflush() function takes one required parameter:

  • $stream: The file pointer to flush. It must be a valid, writable stream resource — typically one returned by fopen(). Streams opened read-only (mode 'r') have nothing to flush.

Return Value

fflush() returns true on success or false on failure. It fails when the resource is not a valid open stream or the underlying write cannot be completed; a warning may be emitted. Because failures are rare but real (a full disk, a broken pipe), check the return value when the write must not be lost:

<?php

if (fflush($stream) === false) {
    // The buffered data could not be written — handle it (log, retry, abort).
}

Examples

Example 1: Flush a file pointer

Open a file, write data, then flush so the bytes hit the stream before the script continues:

Flush a file pointer in PHP

<?php

$fileHandle = fopen('example.txt', 'w');
fwrite($fileHandle, 'Hello, World!');

if (fflush($fileHandle)) {
    echo "Buffer flushed to the stream.\n";
}

fclose($fileHandle);

Output:

Buffer flushed to the stream.

Example 2: A continuously flushed log

A worker that appends to a log file and flushes after every entry, so another process can tail -f the file and see lines appear in real time instead of in bursts:

Real-time logging with fflush()

<?php

$log = fopen('worker.log', 'a');

foreach (['started', 'processing', 'done'] as $event) {
    fwrite($log, date('c') . " {$event}\n");
    fflush($log); // each line is visible immediately, not only at fclose()
}

fclose($log);

Without the fflush() call, the three lines would normally appear only when the buffer fills or when fclose() runs at the end.

fflush() vs. fclose()

You do not need fflush() right before fclose()fclose() flushes any remaining buffered data automatically before closing the resource. Use fflush() only when you need the data written while the file is still open.

fflush() does not guarantee the data is on disk

This is the most common misconception. fflush() pushes PHP's buffer down to the operating system, but the OS keeps its own write cache. After fflush() the bytes may still live in the OS cache rather than on the physical disk. If the machine loses power immediately after, the data can be lost.

To force the OS to commit the data to durable storage, follow up with fsync() (PHP 8.1+):

<?php

$file = fopen('important.txt', 'w');
fwrite($file, 'critical data');
fflush($file); // PHP buffer -> OS
fsync($file);  // OS cache -> physical disk (PHP 8.1+)
fclose($file);

Controlling the buffer itself

If you want to change how much PHP buffers before it auto-writes — rather than flushing on demand — use set_file_buffer(). Setting the buffer size to 0 disables buffering entirely, so every fwrite() is written through without needing an explicit fflush().

Note on output buffering

Do not confuse fflush() with PHP's output-buffering functions like ob_flush() and flush(). fflush() operates on a file/stream resource (files, sockets, pipes). ob_flush() flushes PHP's internal output buffer (the page body) toward the web server or client. They are unrelated mechanisms that happen to share the word "flush".

Conclusion

In conclusion, the fflush() function is a useful PHP function that ensures buffered data is immediately written to a file stream. It is essential for applications requiring real-time data persistence, such as logging systems or data processing scripts.

By using the examples provided in this article, you should now be able to use the fflush() function in your PHP code with ease. If you have any questions or concerns about using the fflush() function in PHP, feel free to reach out to us. We'd be happy to help you out.

Practice

Practice
What is the correct usage of the fflush() function in PHP?
What is the correct usage of the fflush() function in PHP?
Was this page helpful?