Latest web development tutorials

PHP fseek () function

PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual

Definition and Usage

fseek () function located in the open file.

This function resets the file pointer from the current position or move forward and backward to the new location, the new location from the beginning of the file measured in bytes.

The function returns 0 if successful, or -1 if it fails. Please note that position to the end of the file (EOF) After no errors.

grammar

fseek(file,offset,whence)

参数 描述
file 必需。规定要在其中定位的文件。
offset 必需。规定新的位置(从文件头开始以字节数度量)。
whence 可选。(PHP 4 中新增的)。 可能的值:
  • SEEK_SET - 设定位置等于 offset。默认。
  • SEEK_CUR - 设定位置为当前位置加上 offset。
  • SEEK_END - 设定位置为文件末尾(EOF)加上 offset(要移动到文件末尾之前的位置,offset 必须是一个负值)。


Tips and Notes

Tip: By using ftell () to find the current location!


Examples

<?php
$file = fopen("test.txt","r");
// read first line
fgets($file);
// move back to beginning of file
fseek($file,0);
?>


PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual