PHP fflush() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fflush() forces any data waiting in the write buffer of an open file to be written out. It makes sure everything you wrote with fwrite() has been handed to the file.

PHP may hold your data in a buffer for speed before it reaches the file. fflush() empties that buffer on demand, which is useful before other code reads the file or before you release a lock.

Syntax

Call fflush() with the values you need. Start with this line.

PHP
$ok = fflush($handle);
CallWhat it does
fflush($handle)Writes the buffered data to the file and returns true on success.
fwrite($handle, $text); fflush($handle);Write something, then make sure it is saved right away.
fflush($handle); flock($handle, LOCK_UN);Flush before you release a file lock so other scripts see the full data.

fflush() only empties PHP's buffer. It does not guarantee the data is on the physical disk. For that, call fdatasync() or fsync() afterwards (PHP 8.1 and later).

Library declarations
PHP
fflush(resource $stream): bool

Parameters

ParameterDescription
streamRequired. An open file stream, such as the one returned by fopen(). The file must be open for writing.

Return Value

Return ValueDescription
booltrue on success, or false on failure.

PHP Examples

Example 1 — Flush Before Reading

Write text to a file, flush it and read the file back while the handle is still open.

PHP
<?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

  1. fopen() opens the temporary file in write mode.
  2. fwrite() adds the word Hello to PHP's write buffer.
  3. fflush() pushes the buffered data into the file right away.
  4. file_get_contents() can now read Hello, even though the handle is still open.

Example 2 — Flushing a Closed Stream

After fclose() the handle is no longer valid. In PHP 8, fflush() throws a TypeError, so catch it if the handle might be closed.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fflush_edge.txt';

$handle = fopen($path, 'w');
fclose($handle);

try {
    fflush($handle);
} catch (TypeError $e) {
    echo 'Cannot flush a closed stream', PHP_EOL;
}

unlink($path);

How It Works

  1. fclose() closes the file, so $handle no longer points to an open stream.
  2. fflush() receives an invalid resource and throws a TypeError in PHP 8.
  3. The catch block handles the error and prints a friendly message.
  4. unlink() removes the empty demo file.

Example 3 — Flush Inside a File Lock

Lock the file, write, flush and then unlock. This is the safe pattern when several scripts share a file.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_fflush_lock.txt';
@unlink($path);

$handle = fopen($path, 'a');

if (flock($handle, LOCK_EX)) {
    fwrite($handle, 'Visitor 1' . PHP_EOL);
    fflush($handle);
    flock($handle, LOCK_UN);
}

fclose($handle);

echo file_get_contents($path);
unlink($path);

How It Works

  1. flock() with LOCK_EX asks for exclusive access, so other scripts must wait.
  2. fwrite() adds the line, and fflush() writes it out before the lock is released.
  3. flock() with LOCK_UN gives the file back to other scripts.
  4. The final echo shows that the line was saved correctly.

Frequently Asked Questions

It writes any data waiting in the stream's write buffer to the file. After it returns true, everything you wrote with fwrite() has been handed to the file.
No. fclose() flushes the buffer for you. You mostly need fflush() when you keep the file open, for example before unlocking it.
Not necessarily. It only empties PHP's buffer. Use fdatasync() or fsync() when you must be sure the data reached the disk.
The handle you passed is not a valid open stream, usually because you already called fclose(). PHP 8 throws a TypeError in that case.
Call fflush() just before flock($handle, LOCK_UN). This makes sure other scripts see all of your data once the lock is released.

Next: fgetc()

fgetc() reads a single character from an open file. It returns that one character as a str.

fgetc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful