PHP zip_read() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_read() returns the next entry from an archive opened with zip_open(), one entry per call.

It is deprecated as of PHP 8.0. With ZipArchive, loop from 0 to numFiles, or call statIndex() until it returns false, to walk through every entry.

Syntax

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

PHP
// Deprecated (PHP 8.0+)
while ($entry = zip_read($zip)) {
    echo zip_entry_name($entry), PHP_EOL;
}

// Modern equivalent
for ($i = 0; $i < $zip->numFiles; $i++) {
    echo $zip->getNameIndex($i), PHP_EOL;
}
CallWhat it does
zip_read($zip)Deprecated: returns the next entry resource, or false when there are no more entries.
@zip_read($zip)Deprecated: the @ operator hides the E_DEPRECATED notice only.
$zip->statIndex($i)Modern ZipArchive: returns an array of details for the entry at that index, or false past the end.

zip_read() is deprecated in PHP 8.0. It moved an internal pointer forward each time you called it. ZipArchive uses plain numeric indexes, so you can read any entry in any order.

Library declarations
PHP
zip_read(resource $zip): resource|false

Parameters

ParameterDescription
zipRequired. The ZIP resource returned by zip_open().

Return Value

Return ValueDescription
resourceA directory entry resource for the next entry.
falseThere are no more entries, or an error happened.

PHP Examples

Example 1 — List Entries With a for Loop

Loop through every entry in a temp archive and print its name and size. The old while loop is a comment.

PHP
<?php
function makeDemoZip(): string
{
    $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        die("Cannot create zip\n");
    }
    $zip->addFromString('hello.txt', "Hello zip\n");
    $zip->addFromString('docs/readme.txt', "Read me\n");
    $zip->close();
    return $path;
}

// Legacy (deprecated since PHP 8.0):
// while ($entry = zip_read($zip)) {
//     echo zip_entry_name($entry);
// }

$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);

for ($i = 0; $i < $zip->numFiles; $i++) {
    $stat = $zip->statIndex($i);
    echo $stat['name'], ' (', $stat['size'], ' bytes)', PHP_EOL;
}

$zip->close();
unlink($path);

How It Works

  1. Create and open the demo archive.
  2. Loop from zero to numFiles.
  3. Call statIndex() to get the name and size.
  4. Print each entry, then close and delete the archive.

Example 2 — Stop When statIndex() Returns false

This while loop works like zip_read(): it keeps going until there is no next entry.

PHP
<?php
function makeDemoZip(): string
{
    $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        die("Cannot create zip\n");
    }
    $zip->addFromString('hello.txt', "Hello zip\n");
    $zip->addFromString('docs/readme.txt', "Read me\n");
    $zip->close();
    return $path;
}

$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);

$i = 0;
while (($stat = $zip->statIndex($i)) !== false) {
    echo 'Entry ', $i, ': ', $stat['name'], PHP_EOL;
    $i++;
}
echo 'Done after ', $i, ' entries', PHP_EOL;

$zip->close();
unlink($path);

How It Works

  1. Open the demo archive and start an index at zero.
  2. Call statIndex() in the while condition.
  3. Print each name and add one to the index.
  4. Stop when statIndex() returns false and print the total.

Example 3 — Read Each Entry's Text

Walk through all entries and read their contents with getFromIndex().

PHP
<?php
function makeDemoZip(): string
{
    $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        die("Cannot create zip\n");
    }
    $zip->addFromString('hello.txt', "Hello zip\n");
    $zip->addFromString('docs/readme.txt', "Read me\n");
    $zip->close();
    return $path;
}

$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);

for ($i = 0; $i < $zip->numFiles; $i++) {
    $name = $zip->getNameIndex($i);
    $text = trim($zip->getFromIndex($i));
    echo $name, ': ', $text, PHP_EOL;
}

$zip->close();
unlink($path);

How It Works

  1. Create and open the demo archive.
  2. Loop over each index up to numFiles.
  3. Get the name with getNameIndex() and the text with getFromIndex().
  4. Print name and text together, then clean up.

Frequently Asked Questions

Yes. It is part of the procedural zip_* API that is deprecated as of PHP 8.0. Use ZipArchive with numFiles and statIndex() or getNameIndex().
Use for ($i = 0; $i < $zip->numFiles; $i++) and call getNameIndex($i) or statIndex($i) inside the loop.
Yes. ZipArchive uses numeric indexes and names, so you can call getFromName() or locateName() directly. zip_read() only moved forward.
It returns false when no more entries are left, which is why the old loop was written as while ($entry = zip_read($zip)).
Read the numFiles property of your ZipArchive object, or call count($zip).

Back to Zip Functions

Browse all PHP zip function tutorials.

Zip functions →

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