Content · Programming · PHP

PHP fdatasync() Function: Usage and Examples

Learn how PHP fdatasync works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Requests that file data be synchronized from buffers to persistent storage.

What Is fdatasync() in PHP?

Requests that file data be synchronized from buffers to persistent storage.

Its syntax is:

fdatasync(resource $stream): bool

Parameters:

  • $stream — Accepted as resource; required argument.

Return type: bool. Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Availability: Available in PHP 8.1+; stream and filesystem support vary.

Example 1: Basic Usage

<?php

$stream = tmpfile(); fwrite($stream, "report"); var_dump(fdatasync($stream)); fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Example 2: A Different Input

<?php

$stream = tmpfile(); fwrite($stream, "hello"); fflush($stream); echo fdatasync($stream) ? "Synced" : "Failed"; fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Practical Example: Applying It in a Program

<?php

$stream = tmpfile(); fwrite($stream, "invoice-data"); if (!fdatasync($stream)) { echo "Unable to sync data"; } else { echo "Data sync requested"; } fclose($stream);

Result: Results and availability depend on the current PHP process, system permissions, cache, filesystem or installed locale. Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Important Behavior and Edge Cases

  • Key behavior: Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.
  • Environment: Available in PHP 8.1+; stream and filesystem support vary.

Comparison With Related Functions

FunctionPurpose
fdatasync()Requests that file data be synchronized from buffers to persistent storage.
fsync()fsync() asks to synchronize file data and metadata; fdatasync() concentrates on data and required metadata.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Requires a suitable writable file streamRequires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Key Takeaways

  • Requests that file data be synchronized from buffers to persistent storage.
  • Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.
  • Available in PHP 8.1+; stream and filesystem support vary.

Summary: Requires a suitable writable file stream. Synchronization does not make a multi-step transaction atomic.

Share this resourceSend this page to someone who may find it useful.