(PHP 4, PHP 5, PHP 7, PHP 8)
fseek — 在文件指針中定位
$handle
, int $offset
, int $whence
= SEEK_SET): int
在與
handle
關(guān)聯(lián)的文件中設(shè)定文件指針位置。
新位置從文件頭開始以字節(jié)數(shù)度量,是以
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.
handle
文件系統(tǒng)指針,是典型地由 fopen() 創(chuàng)建的 resource(資源)。
offset
偏移量。
要移動到文件尾之前的位置,需要給
offset
傳遞一個負(fù)值,并設(shè)置 whence
為 SEEK_END
。
whence
whence
values are:
SEEK_SET
- 設(shè)定位置等于 offset
字節(jié)。SEEK_CUR
- 設(shè)定位置為當(dāng)前位置加上 offset
。SEEK_END
- 設(shè)定位置為文件尾加上
offset
。成功則返回 0;否則返回 -1。注意移動到 EOF 之后的位置不算錯誤。
示例 #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+
),任何寫入文件數(shù)據(jù)都會被附加上去,而文件的位置將會被忽略,調(diào)用 fseek() 的結(jié)果尚未定義。
注意:
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.