PHP Velho Oeste 2024

fsync

(PHP 8 >= 8.1.0)

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

説明

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() - 出力をファイルにフラッシュする

add a note add a note

User Contributed Notes 1 note

up
9
Dave Gebler
2 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.
To Top