Content · Programming · PHP

PHP fseek() Function: Usage and Examples

Moves a stream cursor to an offset when the stream supports seeking.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

fseek() Function is one of the PHP operations used in filesystem and stream workflows. Moves a stream cursor to an offset when the stream supports seeking. The examples below show what it does and how to handle its result.

What Is fseek() Function?


Moves a stream cursor to an offset when the stream supports seeking.

Its syntax is:

fseek($stream, int $offset, int $whence = SEEK_SET)

The parameters are:

  • $stream — An open stream resource.
  • int $offset — Byte offset or position from the chosen origin.
  • int $whence = SEEK_SET — Offset origin: SEEK_SET, SEEK_CUR, or SEEK_END.

Return value: The declared return type is int. A non-seekable network stream may reject fseek(); always check the result.

Important: A non-seekable network stream may reject fseek(); always check the result.

Example 1: Basic Usage


The basic example focuses on the behavior of fseek() Function: moves a stream cursor to an offset when the stream supports seeking. Inputs are defined locally or shown as application-controlled paths.

<?php

$stream = fopen("php://memory", "w+");
fwrite($stream, "ABCDEF");
fseek($stream, 3, SEEK_SET);
echo fread($stream, 3);
fclose($stream);

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for fseek() Function. Notice the different checks, arguments, or output handling.

<?php

$stream = fopen(__FILE__, "rb");
if (fseek($stream, 0, SEEK_END) === 0) { echo ftell($stream); }
fclose($stream);

This variation matters because a non-seekable network stream may reject fseek(); always check the result.

Practical Example: Using the Operation in an Application


The following example demonstrates how fseek() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$stream = fopen(__DIR__ . "/archive.bin", "rb");
if ($stream === false) { throw new RuntimeException("Cannot open file"); }
try { if (fseek($stream, -4, SEEK_END) === 0) { echo bin2hex(fread($stream, 4)); } } finally { fclose($stream); }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Moves a stream cursor to an offset when the stream supports seeking.
  • Limitation: A non-seekable network stream may reject fseek(); always check the result.

Comparison With Related Functions


Function or MethodDifference
fseek() FunctionMoves a stream cursor to an offset when the stream supports seeking.
rewind()Returns the stream cursor to byte offset zero; fseek() accepts offsets and origins.

Use fseek() Function for the specific behavior described above. The related operation, rewind(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Treating stream or file operations as guaranteed complete on the first call.A non-seekable network stream may reject fseek(); always check the result. Handle false returns, partial I/O, stream position and closing resources.
Treating rewind() as an interchangeable replacement for fseek() Function.Returns the stream cursor to byte offset zero; fseek() accepts offsets and origins.

Key Takeaways


  • Moves a stream cursor to an offset when the stream supports seeking.
  • A non-seekable network stream may reject fseek(); always check the result.
  • Related operation: rewind() — Returns the stream cursor to byte offset zero; fseek() accepts offsets and origins.

fseek() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.