(PHP 4, PHP 5, PHP 7, PHP 8)
fseek — 在文件指针中定位
在与 stream
关联的文件中设定文件指针位置。新位置从文件头开始以字节数度量,是以
whence
指定的位置加上 offset
。
In general, it is allowed to seek past the end-of-file; if data is then written, reads in any unwritten region between the end-of-file and the sought position will yield bytes with value 0. However, certain streams may not support this behavior, especially when they have an underlying fixed size storage.
成功则返回 0;否则返回 -1。
示例 #1 fseek() 例子
<?php
$fp = fopen('somefile.txt', 'r');
// read some data
$data = fgets($fp, 4096);
// move back to the beginning of the file
// same as rewind($fp);
fseek($fp, 0);
?>
注意:
如果使用附加模试(
a
或a+
),任何写入文件数据都会被附加上去,而文件的位置将会被忽略,调用 fseek() 的结果尚未定义。
注意:
Not all streams support seeking. For those that do not support seeking, forward seeking from the current position is accomplished by reading and discarding data; other forms of seeking will fail.