rewind()函数可以将文件指针的位置回退到文件的开头,成功时返回true,失败时返回false。
bool rewind ( resource $handle )
将 handle 的文件位置指针设为文件流的开头。
注意:如果将文件以追加("a" 或者 "a+")模式打开,写入文件的任何数据总是会被附加在后面,不管文件指针的位置。
<?php
$handle = fopen("/PhpProject/sample.txt", "r+");
fwrite($handle, "Long sentence");
rewind($handle);
fwrite($handle, "Hello PHP");
rewind($handle);
echo fread($handle, filesize("/PhpProject/sample.txt"));
fclose($handle);
?>输出结果
Hello PHPence
<?php
$file = fopen("/PhpProject/sample.txt", "r");
fseek($file, "15"); // 更改文件指针的位置
rewind($file); // 将文件指针设置为0
fclose($file);
?>