fseek

(PHP 3, PHP 4, PHP 5)

fseek -- 在檔案指標中定位

說明

int fseek ( resource handle, int offset [, int whence] )

在與 handle 關聯的檔案中設定檔案指標位置。新位置從檔案頭開始以位元組數度量,是以 whence 特殊的位置加上 offsetwhence 的值定義為:

SEEK_SET - 設定位置等於 offset 位元組。
SEEK_CUR - 設定位置為現用的位置加上 offset
SEEK_END - 設定位置為檔案尾加上 offset。(要搬移到檔案尾之前的位置,需要給 offset 傳遞一個負值。)

若果沒有指定 whence,預設為 SEEK_SET

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

例子 1. fseek() 例子

<?php

$fp 
fopen('somefile.txt');

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

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

?>

可能不能用於在 fopen() 中以 "http://" 或 "ftp://" 格式開啟所返回的檔案指標。在附加模式(加參數 "a" 開啟檔案)中 ftell() 會返回未定義錯誤。

注: whence 參數是 PHP 4.0.0 之後增加的。

注: 若果使用附加模試("a" 或 "a+"),任何寫入檔案資料都會被附加上去,而檔案的位置將會被忽略。

參見 ftell()rewind()