PHP fdatasync() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fdatasync() forces the data you wrote to an open file to be saved on the physical disk. It is available in PHP 8.1 and later.

When you call fwrite(), the operating system may keep the data in memory for a while. fdatasync() asks the system to write it to disk now, so a crash or power cut does not lose it. It is a lighter version of fsync().

Syntax

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

PHP
$ok = fdatasync($handle);
CallWhat it does
fdatasync($handle)Returns true when the file data reached the disk, false on failure.
fflush($handle); fdatasync($handle);Flush PHP's own buffer first, then ask the system to save the data.
if (!fdatasync($handle)) { ... }Check the result so you can react when the sync fails.

fdatasync() needs PHP 8.1 or later. It saves the file data but may skip some metadata, such as the modification time, so it is faster than fsync(). It only works on streams backed by a real file.

Library declarations
PHP
fdatasync(resource $stream): bool

Parameters

ParameterDescription
streamRequired. An open file stream, such as the one returned by fopen(). It must be a real file, not php://memory.

Return Value

Return ValueDescription
booltrue on success, or false if the stream cannot be synced.

PHP Examples

Example 1 — Save Data to Disk

Write a small record to a file, flush it and ask the system to store it safely.

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

$handle = fopen($path, 'w');
fwrite($handle, 'Order 1001 paid');
fflush($handle);

$ok = fdatasync($handle);
echo 'Synced to disk: ', var_export($ok, true), PHP_EOL;

fclose($handle);
unlink($path);

How It Works

  1. fopen() opens a temporary file for writing.
  2. fwrite() adds the text, and fflush() empties PHP's own buffer.
  3. fdatasync() asks the operating system to write the data to the disk.
  4. var_export() prints the boolean result, and unlink() removes the demo file.

Example 2 — A Stream That Cannot Be Synced

A memory stream has no file on disk, so there is nothing to sync. The function returns false.

PHP
<?php
$handle = fopen('php://memory', 'w+');
fwrite($handle, 'Hello');

$ok = @fdatasync($handle);
echo 'Memory stream synced: ', var_export($ok, true), PHP_EOL;

fclose($handle);

How It Works

  1. php://memory keeps its data in RAM, so it is not a real file.
  2. fdatasync() cannot sync it and returns false (PHP also raises a warning).
  3. The @ operator hides the warning so the output stays clean.
  4. Always check the return value instead of assuming the data is safe.

Example 3 — Guard for Older PHP Versions

Because fdatasync() only exists in PHP 8.1 and later, check for it with function_exists() before calling it.

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

$handle = fopen($path, 'w');
$bytes = fwrite($handle, 'Order 1001 paid');
fflush($handle);

echo 'Wrote ', $bytes, ' bytes', PHP_EOL;

if (function_exists('fdatasync')) {
    fdatasync($handle);
    echo 'Data flushed to disk', PHP_EOL;
} else {
    echo 'fdatasync() needs PHP 8.1 or later', PHP_EOL;
}

fclose($handle);
unlink($path);

How It Works

  1. fwrite() returns the number of bytes written, which is 15 here.
  2. fflush() makes sure PHP has handed all data to the system.
  3. function_exists() checks that fdatasync() is available on this PHP version.
  4. On PHP 8. 1 and later the sync runs, and the success message is printed.

Frequently Asked Questions

It asks the operating system to write the data of an open file to the physical disk. This protects the data if the computer crashes or loses power.
fsync() saves the file data and all its metadata, while fdatasync() saves the data and only the metadata needed to read it back. This can make fdatasync() a little faster.
Yes. fdatasync() does not flush PHP's own buffer. Call fflush() first so every byte reaches the system before it is synced.
PHP 8.1. If your code must run on older versions, wrap the call in a function_exists('fdatasync') check.
The stream is probably not a real file, for example php://memory or php://output, or the system could not complete the sync.

Next: feof()

feof() tells you whether the file pointer of an open file has reached the end of the file.

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