PHP fseek() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fseek() moves the file pointer of an open file to a new position. The pointer is the invisible marker that tells PHP where the next read or write will happen.

You pass the open file handle, the number of bytes to move, and where to count from. It returns 0 when the move works and -1 when it fails, which is the opposite of what many beginners expect.

Syntax

Call fseek() with the values you need. Start with this line.

PHP
$result = fseek($fp, 6);
CallWhat it does
fseek($fp, 6)Moves the pointer to byte 6 counted from the start of the file.
fseek($fp, -3, SEEK_END)Moves the pointer to 3 bytes before the end of the file.
fseek($fp, 2, SEEK_CUR)Moves the pointer 2 bytes forward from where it is now.

fseek() returns 0 on success and -1 on failure. Do not write if (fseek(...)) to test for success, because 0 counts as false in PHP. Compare with === 0 instead.

Library declarations
PHP
fseek(resource $stream, int $offset, int $whence = SEEK_SET): int

Parameters

ParameterDescription
streamRequired. A file handle returned by fopen(). The file must still be open.
offsetRequired. The number of bytes to move. It can be negative when you use SEEK_CUR or SEEK_END.
whenceOptional. Where to count from: SEEK_SET (start of file, the default), SEEK_CUR (current position) or SEEK_END (end of file).

Return Value

Return ValueDescription
intReturns 0 on success and -1 on failure. Seeking past the end of a file is allowed and still returns 0.

PHP Examples

Example 1 — Jump to a Position and Read

Write a short text file, then skip the first 6 bytes and read the rest.

PHP
<?php
$path = sys_get_temp_dir() . '/fseek_demo.txt';
file_put_contents($path, 'Hello World');

$fp = fopen($path, 'r');
fseek($fp, 6);
echo fread($fp, 5), PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. file_put_contents() creates a small file that contains Hello World.
  2. fopen() opens the file for reading, and the pointer starts at byte 0.
  3. fseek($fp, 6) moves the pointer past Hello and the space, so it now sits on the letter W.
  4. fread() reads 5 bytes from that new position and prints World.

Example 2 — Check for a Failed Seek

A negative offset from the start of the file is not allowed. fseek() returns -1 and the pointer stays where it was.

PHP
<?php
$path = sys_get_temp_dir() . '/fseek_edge.txt';
file_put_contents($path, 'Hello World');

$fp = fopen($path, 'r');

if (fseek($fp, -5) === -1) {
    echo "Seek failed", PHP_EOL;
}
echo "Position is still ", ftell($fp), PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. The default whence is SEEK_SET, so -5 would mean a spot before the start of the file.
  2. fseek() cannot do that, so it returns -1 instead of moving the pointer.
  3. The test uses === -1 so that the success value 0 is never mistaken for a failure.
  4. ftell() confirms that the pointer did not move and is still at position 0.

Example 3 — Use SEEK_SET, SEEK_END and SEEK_CUR

Move around the same file with all three whence values and read a few bytes each time.

PHP
<?php
$path = sys_get_temp_dir() . '/fseek_modes.txt';
file_put_contents($path, 'ABCDEFGHIJ');

$fp = fopen($path, 'r');

fseek($fp, -3, SEEK_END);
echo fread($fp, 3), PHP_EOL;

fseek($fp, 2, SEEK_SET);
echo fread($fp, 2), PHP_EOL;

fseek($fp, 2, SEEK_CUR);
echo fread($fp, 2), PHP_EOL;

fclose($fp);
unlink($path);

How It Works

  1. SEEK_END with -3 counts back three bytes from the end, so the next read gives HIJ.
  2. SEEK_SET with 2 jumps to byte 2 from the start, and reading two bytes gives CD.
  3. After reading CD the pointer is at byte 4. SEEK_CUR with 2 skips EF and lands on G.
  4. The last fread() returns GH.

Frequently Asked Questions

It moves the file pointer of an open file to a new position. The next fread(), fgets() or fwrite() call then starts from that position.
It returns 0 when the seek works and -1 when it fails. Because 0 is falsy in PHP, test the result with === 0 or === -1.
Use fseek($fp, 0) or the shorter rewind($fp). Both place the pointer at byte 0 so you can read the file again.
Yes. PHP allows it and returns 0. If you then write there, the gap between the old end and the new data is filled with null bytes.
Call fseek($fp, -10, SEEK_END) to move 10 bytes before the end, then fread($fp, 10). This avoids loading the whole file.

Next: fstat()

fstat() returns information about an open file, such as its size, permissions and last mod.

fstat() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful