目录

PHP fseek() 函数

❮ PHP 文件系统参考

示例

从打开的文件中读取第一行,然后将文件指针移回到文件的开头:

<?php
$file = fopen("test.txt","r");
// Read first line
echo fgets($file);
// Move back to beginning of file
fseek($file,0);
fclose($file);
?>
运行示例 »

定义和用法

fseek() 函数在打开的文件中查找。

此函数将文件指针从当前位置移动到新位置(向前或向后,由字节数指定)。

提示:您可以使用ftell()找到当前位置!

语法

fseek( file, offset, whence)

参数值

Parameter Description
file Required. Specifies the open file to seek in
offset Required. Specifies the new position (measured in bytes from the beginning of the file)
whence Optional. Possible values:
  • SEEK_SET - Set position equal to offset. Default
  • SEEK_CUR - Set position to current location plus offset
  • SEEK_END - Set position to EOF plus offset (to move to a position before EOF, the offset must be a negative value)


技术细节

返回值: 成功时为 0,否则为 -1
PHP 版本: 4.0+

❮ PHP 文件系统参考