|
| 1 | +--TEST-- |
| 2 | +Test fsync() function: basic functionality |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +echo "*** Testing fsync(): writing to a file and reading the contents ***\n"; |
| 7 | +$data = <<<EOD |
| 8 | +first line of string |
| 9 | +second line of string |
| 10 | +third line of string |
| 11 | +EOD; |
| 12 | + |
| 13 | +$file_path = __DIR__; |
| 14 | +$filename = "$file_path/fsync_basic.tmp"; |
| 15 | + |
| 16 | +// opening a file |
| 17 | +$file_handle = fopen($filename, "w"); |
| 18 | +if($file_handle == false) |
| 19 | + exit("Error:failed to open file $filename"); |
| 20 | + |
| 21 | +if(substr(PHP_OS, 0, 3) == "WIN") { |
| 22 | + $data = str_replace("\r",'', $data); |
| 23 | +} |
| 24 | + |
| 25 | +// writing data to the file |
| 26 | +var_dump( fwrite($file_handle, $data) ); |
| 27 | +var_dump( fsync($file_handle) ); |
| 28 | +var_dump( readfile($filename) ); |
| 29 | + |
| 30 | +echo "\n*** Testing fsync(): for return type ***\n"; |
| 31 | +$return_value = fsync($file_handle); |
| 32 | +var_dump( is_bool($return_value) ); |
| 33 | +fclose($file_handle); |
| 34 | + |
| 35 | +echo "\n*** Testing fsync(): for non-file stream ***\n"; |
| 36 | +$file_handle = fopen("php://memory", "w"); |
| 37 | +$return_value = fsync($file_handle); |
| 38 | +var_dump( ($return_value) ); |
| 39 | +fclose($file_handle); |
| 40 | + |
| 41 | +echo "\n*** Done ***"; |
| 42 | +?> |
| 43 | +--CLEAN-- |
| 44 | +<?php |
| 45 | +$file_path = __DIR__; |
| 46 | +$filename = "$file_path/fsync_basic.tmp"; |
| 47 | +unlink($filename); |
| 48 | +?> |
| 49 | +--EXPECT-- |
| 50 | +*** Testing fsync(): writing to a file and reading the contents *** |
| 51 | +int(63) |
| 52 | +bool(true) |
| 53 | +first line of string |
| 54 | +second line of string |
| 55 | +third line of stringint(63) |
| 56 | + |
| 57 | +*** Testing fsync(): for return type *** |
| 58 | +bool(true) |
| 59 | + |
| 60 | +*** Testing fsync(): for non-file stream *** |
| 61 | +Warning: fsync(): Can't fsync this stream! |
| 62 | +bool(false) |
| 63 | + |
| 64 | +*** Done *** |
0 commit comments