在檔案指針中定位

fseek

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

fseek在檔案指針中定位

說明

fseek(resource $handle, int $offset, int $whence = SEEK_SET): int

在與 handle 關聯的檔案中設定檔案指針位置。 新位置從檔案頭開始以位元組數度量,是以 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

檔案系統指針,是典型地由 fopen() 建立的 resource(資源)。

offset

偏移量。

要移動到檔案尾之前的位置,需要給 offset 傳遞一個負值,並設定 whenceSEEK_END

whence

whence values are:

  • SEEK_SET - 設定位置等於 offset 位元組。
  • SEEK_CUR - 設定位置為目前位置加上 offset
  • SEEK_END - 設定位置為檔案尾加上 offset

返回值

成功則返回 0;否則返回 -1。注意移動到 EOF 之後的位置不算錯誤。

範例

示例 #1 fseek() 例子

<?php

$fp 
fopen('somefile.txt''r');

// read some data
$data fgets($fp4096);

// move back to the beginning of the file
// same as rewind($fp);
fseek($fp0);

?>

註釋

注意:

如果使用附加模試(aa+),任何寫入檔案數據都會被附加上去,而檔案的位置將會被忽略,呼叫 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.

參見

  • ftell() - 返回檔案指針讀/寫的位置
  • rewind() - 倒回檔案指針的位置

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *