xref: /PHP-8.3/ext/standard/tests/file/fsync.phpt (revision cbcfd860)
1--TEST--
2Test fsync() function: basic functionality
3--FILE--
4<?php
5
6echo "*** Testing fsync(): writing to a file and reading the contents ***\n";
7$data = <<<EOD
8first line of string
9second line of string
10third line of string
11EOD;
12
13$file_path = __DIR__;
14$filename = "$file_path/fsync_basic.tmp";
15
16// opening a file
17$file_handle = fopen($filename, "w");
18if($file_handle == false)
19  exit("Error:failed to open file $filename");
20
21if(PHP_OS_FAMILY == 'Windows')  {
22    $data = str_replace("\r",'', $data);
23}
24
25// writing data to the file
26var_dump( fwrite($file_handle, $data) );
27var_dump( fsync($file_handle) );
28var_dump( readfile($filename) );
29
30echo "\n*** Testing fsync(): for return type ***\n";
31$return_value = fsync($file_handle);
32var_dump( is_bool($return_value) );
33fclose($file_handle);
34
35echo "\n*** Testing fsync(): attempting to sync stdin ***\n";
36$file_handle = fopen("php://stdin", "w");
37var_dump(fsync($file_handle));
38fclose($file_handle);
39
40echo "\n*** Testing fsync(): for non-file stream ***\n";
41$file_handle = fopen("php://memory", "w");
42$return_value = fsync($file_handle);
43var_dump( ($return_value) );
44fclose($file_handle);
45
46echo "\n*** Done ***";
47?>
48--CLEAN--
49<?php
50$file_path = __DIR__;
51$filename = "$file_path/fsync_basic.tmp";
52unlink($filename);
53?>
54--EXPECTF--
55*** Testing fsync(): writing to a file and reading the contents ***
56int(63)
57bool(true)
58first line of string
59second line of string
60third line of stringint(63)
61
62*** Testing fsync(): for return type ***
63bool(true)
64
65*** Testing fsync(): attempting to sync stdin ***
66bool(false)
67
68*** Testing fsync(): for non-file stream ***
69
70Warning: fsync(): Can't fsync this stream! in %s on line %d
71bool(false)
72
73*** Done ***
74