PHP fseek() function

Definition and Usage

fseek() function locates in the opened file.

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

Returns 0 on success; otherwise returns -1. Note that moving to a position after EOF does not cause 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 position to offset bytes. Default.
  • SEEK_CUR - Set position to the current position plus offset.
  • SEEK_END - Set position to the end of the file plus offset (To move to a position before the end of the file,offset MUST be a negative value).

Kuvaus

whence Parametrit lisättiin PHP 4.0.0 jälkeen.

Vinkit ja huomiot

Vinkki:Käyttämällä ftell() löytääksesi nykyisen sijainnin.

Esimerkki

<?php
$file = fopen("test.txt","r");
// Lue ensimmäinen rivi
fgets($file);
// Palatakseni tiedoston alkuun
fseek($file,0);
?>