PHP 8.3.4 Released!

rewind

(PHP 4, PHP 5, PHP 7, PHP 8)

rewindファイルポインタの位置を先頭に戻す

説明

rewind(resource $stream): bool

stream のファイル位置指示子を、 ファイルストリームの先頭にセットします。

注意:

追記モード ("a" もしくは "a+") でファイルをオープンした場合、 ファイルのポインターの位置とは無関係に、 ファイルに書き込まれるデータは常に追加されます。

パラメータ

stream

ファイルポインタは有効なものでなければならず、 また fopen() で正常にオープンされたファイルを指している必要があります。

戻り値

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

例1 rewind() での上書きの例

<?php
$handle
= fopen('output.txt', 'r+');

fwrite($handle, 'Really long sentence.');
rewind($handle);
fwrite($handle, 'Foo');
rewind($handle);

echo
fread($handle, filesize('output.txt'));

fclose($handle);
?>

上の例の出力は、 たとえば以下のようになります。

Foolly long sentence.

参考

  • fread() - バイナリセーフなファイルの読み込み
  • fseek() - ファイルポインタを移動する
  • ftell() - ファイルの読み書き用ポインタの現在位置を返す
  • fwrite() - バイナリセーフなファイル書き込み処理

add a note

User Contributed Notes 1 note

up
14
MagicalTux at kinoko dot fr
16 years ago
Note that rewind($fd) is exactly the same as fseek($fd, 0, SEEK_SET)

rewind() just moves the location inside the file to the beginning, nothing more. Check if your stream is "seekable" before planning to use fseek/rewind.
To Top