phpday 2027 - Call for Papers

fsync

(PHP 8 >= 8.1.0)

fsync — データをファイルに同期する(メタデータも含む)

説明

function fsync(resource $stream): bool

この関数は、 変更をメタデータも含めてファイルに同期します。 fflush() に似ていますが、 この関数はオペレーティングシステムに対し、 ストレージに書き込むことを指示することも行います。

パラメータ

stream

ファイルポインタは、有効なファイルポインタである必要があり、 fopen() または fsockopen() で正常にオープンされた (そしてまだ fclose() でクローズされていない) ファイルを指している必要があります。

戻り値

成功した場合に true を、失敗した場合に false を返します。

例

例1 fsync() の例

<?php

$file = 'test.txt';

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

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

参考

  • fdatasync() - データをファイルに同期する(但しメタデータは除く)
  • fflush() - 出力をファイルにフラッシュする

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