PHP fsync() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fsync() forces the changes made to an open file to be written to the physical disk. It was added in PHP 8.1.

Normally the operating system keeps written data in memory for a short time. fsync() waits until the data and the file details are really stored, which protects you from losing them in a crash or power cut.

Syntax

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

PHP
$ok = fsync($fp);
CallWhat it does
fsync($fp)Flushes the file data and details to disk and returns true on success.
fflush($fp); fsync($fp)Empties PHP's own buffer first, then asks the system to save to disk.
@fsync($stream)Hides the warning when the stream type does not support syncing.

fsync() needs PHP 8.1 or newer. It can be slow because it waits for the disk, so use it only for data you really cannot afford to lose, such as a payment log. For file content only, see fdatasync().

Library declarations
PHP
fsync(resource $stream): bool

Parameters

ParameterDescription
streamRequired. A file handle returned by fopen(). It must be a stream that supports syncing, such as a normal file.

Return Value

Return ValueDescription
boolReturns true on success or false on failure. It also returns false, with a warning, for streams that cannot be synced.

PHP Examples

Example 1 — Save Data Safely to Disk

Write a line, flush PHP's buffer, then ask the system to store it on disk.

PHP
<?php
$path = sys_get_temp_dir() . '/fsync_demo.log';

$fp = fopen($path, 'w');
fwrite($fp, 'Payment received' . PHP_EOL);
fflush($fp);

if (fsync($fp)) {
    echo "Data saved to disk", PHP_EOL;
}

fclose($fp);
unlink($path);

How It Works

  1. fwrite() puts the text into the file stream.
  2. fflush() pushes PHP's own buffer down to the operating system.
  3. fsync() then asks the system to write that data to the physical disk and returns true.
  4. The if branch runs and prints the confirmation.

Example 2 — Handle a Stream That Cannot Sync

Memory streams are not backed by a disk, so fsync() returns false. The @ sign hides the warning so you can handle it yourself.

PHP
<?php
$fp = fopen('php://memory', 'w+');
fwrite($fp, 'temporary data');

if (@fsync($fp)) {
    echo "Synced", PHP_EOL;
} else {
    echo "fsync is not supported on this stream", PHP_EOL;
}

fclose($fp);

How It Works

  1. php://memory keeps data in RAM only, so there is nothing on disk to sync.
  2. fsync() returns false and would normally also print a warning.
  3. The @ operator hides that warning, and the else branch explains what happened.
  4. Use this pattern only when you expect that a stream might not support syncing.

Example 3 — Write Several Lines Then Sync Once

Syncing after every line is slow. Write all the lines first and sync one time at the end.

PHP
<?php
$path = sys_get_temp_dir() . '/fsync_batch.log';
$lines = ['order 1', 'order 2', 'order 3'];

$fp = fopen($path, 'w');

foreach ($lines as $line) {
    fwrite($fp, $line . PHP_EOL);
}

fflush($fp);
$ok = fsync($fp);

echo "Saved ", count($lines), " lines: ", $ok ? 'synced' : 'not synced', PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. The loop writes each order line to the file with fwrite().
  2. fflush() runs once after the loop and moves everything out of PHP's buffer.
  3. fsync() runs once and stores all three lines on disk together.
  4. count() reports how many lines were written, and the ternary prints synced.

Frequently Asked Questions

It forces the data and details of an open file to be written to the physical disk, so they survive a crash or power loss.
fsync() was added in PHP 8.1. On older versions the function does not exist and calling it causes a fatal error.
fflush() empties PHP's own buffer into the operating system. fsync() goes one step further and makes the system write it to the disk.
fsync() saves both the content and the file details like the modified time. fdatasync() saves mostly the content and can be a little faster.
No. It is slow because it waits for the disk. Use it only for important data, and sync once after a batch of writes.

Next: ftell()

ftell() returns the current position of the file pointer in an open file. The position is .

ftell() 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