PHP fseek() Function

Definition and Usage

fseek() function locates in the opened file.

This function moves the file pointer from the current position to a new position in the file, measured from the beginning of the file in bytes.

Returns 0 on success; otherwise, returns -1. Note that moving to a position after EOF does not produce an error.

Syntax

fseek(file,offset,whence)
Parameters Description
file Required. Specifies the file in which to locate.
offset Required. Specifies the new position (measured in bytes from the beginning of the file).
whence Optional. Possible values:
  • SEEK_SET - Set the position to offset bytes. Default.
  • SEEK_CUR - Set the position to the current position plus offset.
  • SEEK_END - Set the position to the end of the file plus offset (To move to a position before the end of the file,offset It must be a negative value).

Description

whence The parameter was added in PHP 4.0.0 and later.

Tips and Comments

Tip:By using ftell() to find the current position.

Example

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