Example 1 — Save Data to Disk
Write a small record to a file, flush it and ask the system to store it safely.
<?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
- fopen() opens a temporary file for writing.
- fwrite() adds the text, and fflush() empties PHP's own buffer.
- fdatasync() asks the operating system to write the data to the disk.
- var_export() prints the boolean result, and unlink() removes the demo file.

