Content · Programming · PHP

PHP gzpassthru() Function: Usage and Examples

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

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

Use gzpassthru() when you need to outputs the remaining uncompressed contents of a gzip stream in a PHP application. The examples below show the actual operation and its limitations.

What Is gzpassthru() in PHP?

Outputs the remaining uncompressed contents of a gzip stream. The exact signature supported by your installation depends on its PHP version and enabled extensions.

Its syntax is:

gzpassthru($stream): 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,"Hello"); gzclose($file); $file = gzopen($path,"rb"); gzpassthru($file); gzclose($file); unlink($path);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Example 2: Different Input or Behavior

<?php

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

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Practical Example: Applying the Function

<?php

$path = tempnam(sys_get_temp_dir(),"gzip_"); $file = gzopen($path,"wb"); gzwrite($file,"report body"); gzclose($file); $file = gzopen($path,"rb"); $count = gzpassthru($file); gzclose($file); echo "
Bytes: " . $count; unlink($path);

Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.

Important Behavior and Edge Cases

Writes to the output stream; it does not return the extracted content as a string.

Comparison With Related Functions

ApproachDifference
gzpassthru()Outputs the remaining uncompressed contents of a gzip stream.
Related operationgzread() returns uncompressed bytes; gzpassthru() sends them to output.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring the function-specific limitationWrites to the output stream; it does not return the extracted content as a string.
Confusing this function with a related operationgzread() returns uncompressed bytes; gzpassthru() sends them to output.

Key Takeaways

  • Outputs the remaining uncompressed contents of a gzip stream.
  • Writes to the output stream; it does not return the extracted content as a string.
  • gzread() returns uncompressed bytes; gzpassthru() sends them to output.

Use gzpassthru() 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.