Content · Programming · PHP

PHP gzfile() Function: Usage and Examples

Loads a gzip-compressed file into an array of lines.

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

gzfile() Function is one of the PHP operations used in filesystem and stream workflows. Loads a gzip-compressed file into an array of lines. The examples below show what it does and how to handle its result.

What Is gzfile() Function?


Loads a gzip-compressed file into an array of lines.

Its syntax is:

gzfile(string $filename, int $use_include_path = 0)

The parameters are:

  • string $filename — The source or destination filesystem path.
  • int $use_include_path = 0 — When enabled, also searches the configured include_path locations for the requested file.

Return value: The declared return type is array|false. The compressed contents expand in memory; use gzopen()/gzgets() for large streams.

Important: The compressed contents expand in memory; use gzopen()/gzgets() for large streams.

Example 1: Basic Usage


The basic example focuses on the behavior of gzfile() Function: loads a gzip-compressed file into an array of lines. Inputs are defined locally or shown as application-controlled paths.

<?php

$lines = gzfile(__DIR__ . "/sample.log.gz");
if ($lines !== false) { echo count($lines); }

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 gzfile() Function. Notice the different checks, arguments, or output handling.

<?php

$logLines = gzfile(__DIR__ . "/logs.gz", FILE_IGNORE_NEW_LINES);
if ($logLines !== false) { foreach (array_slice($logLines, 0, 3) as $logLine) { echo $logLine . "\n"; } }

This variation matters because the compressed contents expand in memory; use gzopen()/gzgets() for large streams.

Practical Example: Using the Operation in an Application


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

<?php

$compressedLines = gzfile(__DIR__ . "/daily.log.gz", FILE_IGNORE_NEW_LINES);
if ($compressedLines !== false) { foreach (array_slice($compressedLines, -5) as $entry) { echo $entry . "\n"; } }

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: Loads a gzip-compressed file into an array of lines.
  • Limitation: The compressed contents expand in memory; use gzopen()/gzgets() for large streams.

Comparison With Related Functions


Function or MethodDifference
gzfile() FunctionLoads a gzip-compressed file into an array of lines.
gzgets()Streams lines from an opened gzip file; gzfile() loads the entire decompressed list into memory.

Use gzfile() Function for the specific behavior described above. The related operation, gzgets(), 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.The compressed contents expand in memory; use gzopen()/gzgets() for large streams. Handle false returns, partial I/O, stream position and closing resources.
Treating gzgets() as an interchangeable replacement for gzfile() Function.Streams lines from an opened gzip file; gzfile() loads the entire decompressed list into memory.

Key Takeaways


  • Loads a gzip-compressed file into an array of lines.
  • The compressed contents expand in memory; use gzopen()/gzgets() for large streams.
  • Related operation: gzgets() — Streams lines from an opened gzip file; gzfile() loads the entire decompressed list into memory.

gzfile() 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.