导航首页 » 技术教程 » PHP:fseek()的用法_Filesystem函数
PHP:fseek()的用法_Filesystem函数 170 2023-12-15   

fseek

(PHP 4, PHP 5)

fseek — 在文件指针中定位

说明

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

在与 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 - 设定位置为当前位置加上 offsetSEEK_END - 设定位置为文件尾加上 offset

返回值

成功则返回 0;否则返回 -1。注意移动到 EOF 之后的位置不算错误。

范例

Example #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);

?>

注释

Note:

如果使用附加模试(a 或 a+),任何写入文件数据都会被附加上去,而文件的位置将会被忽略,调用 fseek() 的结果尚未定义。

Note:

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() - 倒回文件指针的位置


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103