fseek()
fseek() — Seeks on a file pointer.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Sets the file position indicator for the file referenced by stream. The new position, measured in bytes from the beginning of the file, is obtained by adding offset to the position specified by whence.
Signature
fseek(resource $stream, int $offset, int $whence = SEEK_SET): int
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $stream | resource | A file system pointer resource that is typically created using fopen(). |
| $offset | int | The offset. |
| $whence | int | whence values are: SEEK_SET - Set position equal to offset bytes from the start of the file. SEEK_CUR - Set position to current location plus offset bytes. SEEK_END - Set position to end-of-file plus offset bytes. |
Return value
Upon success, returns 0; otherwise, returns -1. This function was created to imitate the C language function of the same name. Please pay attention to the return values as they differ from what one would expect in PHP.
Example
A runnable snippet:
$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);
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page fseek() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.