phpday 2027 - Call for Papers

fsync

(PHP 8 >= 8.1.0)

fsync — Synchronizes changes to the file (including meta-data)

Description

function fsync(resource $stream): bool

This function synchronizes changes to the file, including its meta-data. This is similar to fflush(), but it also instructs the operating system to write to the storage media.

Parameters

stream

The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

Return Values

Returns true on success or false on failure.

Examples

Example #1 fsync() example

<?php

$file = 'test.txt';

$stream = fopen($file, 'w');
fwrite($stream, 'test data');
fwrite($stream, "\r\n");
fwrite($stream, 'additional data');

fsync($stream);
fclose($stream);
?>

See Also

  • fdatasync() - Synchronizes data (but not meta-data) to the file
  • fflush() - Flushes the output to a file

User Contributed Notes 1 note

up
15
Dave Gebler
5 years ago
Two points worth noting:

1. fsync() is not suitable for high throughput, use it only when the durability of a file write really matters to you.

2. fsync() includes an implicit call to fflush() so you don't need to manually flush before you sync.
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top