Content · Programming · PHP

PHP gzseek() Function: Usage and Examples

Understand PHP gzseek() with three examples, return behavior, important pitfalls, and related operations.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Use gzseek() when you need to moves a gzip stream pointer to an uncompressed-byte offset in a PHP application. The examples below show the actual operation and its limitations.

What Is gzseek() in PHP?

Moves a gzip stream pointer to an uncompressed-byte offset. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gzseek($stream, int $offset, int $whence = ...): int

Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.

Example 1: Basic Usage

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"ABCDE"); gzclose($file); $file = gzopen($path,"rb"); gzseek($file,2); echo gzread($file,3); gzclose($file); unlink($path);

Output:

CDE

Example 2: Different Input or Behavior

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"012345"); gzclose($file); $file = gzopen($path,"rb"); gzseek($file,4); echo gzgetc($file); gzclose($file); unlink($path);

Output:

4

Practical Example: Applying the Function

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"header:payload"); gzclose($file); $file = gzopen($path,"rb"); gzseek($file,7); echo gzread($file,20); gzclose($file); unlink($path);

Output:

payload

Important Behavior and Edge Cases

Seeking within compressed streams may require decompression and can be relatively slow.

Comparison With Related Functions

ApproachDifference
gzseek()Moves a gzip stream pointer to an uncompressed-byte offset.
Related operationgztell() reports the current uncompressed offset; gzseek() changes it.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationSeeking within compressed streams may require decompression and can be relatively slow.
Confusing this function with a related operationgztell() reports the current uncompressed offset; gzseek() changes it.

Key Takeaways

  • Moves a gzip stream pointer to an uncompressed-byte offset.
  • Seeking within compressed streams may require decompression and can be relatively slow.
  • gztell() reports the current uncompressed offset; gzseek() changes it.

Use gzseek() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.

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