Open In App

PHP fdatasync() Function

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The fdatasync() function is an inbuilt function in PHP that is used to synchronize changes to a file's data with the underlying storage device. This function is similar to the fsync() function, but it only synchronizes the file's data, not its metadata.

Syntax:

bool fdatasync(resource $stream)

Parameters: This function takes one parameter which is described below:

  • $stream: A file pointer resource that was obtained using the fopen() function.

Return Value: The fdatasync() function returns true if the synchronization was successful otherwise it will return false.

Example 1: The following program demonstrates the fdatasync() function.

PHP
<?php

$fp = fopen('example.txt', 'w');
fwrite($fp, 'Hello, world!');

if (fdatasync($fp)) {
    echo "Changes to the file's data were successfully synchronized.";
} else {
    echo "Failed to synchronize changes to the file's data.";
}

fclose($fp);

?>

Output:

Changes to the file's data were successfully synchronized. 

Example 2: The following program demonstrates the fdatasync() function.

PHP
<?php
  
$fp = fopen('example.txt', 'w');
fwrite($fp, 'Hello, world!');
  
fdatasync($fp);
fclose($fp);
  
echo "Changes to the file's data were successfully synchronized.";

?>

Output:

Changes to the file's data were successfully synchronized. 

Reference: https://www.php.net/manual/en/function.fdatasync.php


Next Article

Similar Reads