Open In App

PHP fdatasync() Function

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:

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
  
$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
    
$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


Article Tags :