zip_read()
The zip_read() function is a built-in function in PHP that is used to read the entries in a zip archive. The function requires an open file handle, which can be
⚠️ Legacy API Notice: The
zip_read()function and related procedural zip functions were removed in PHP 8.0. They require theext-zipPECL extension and are only available in PHP 7.4 and earlier. For modern PHP applications, theZipArchiveclass is recommended.
The zip_read() function reads the next entry (a single file or directory record) from an open zip archive. It is part of PHP's legacy procedural zip API, where you walk an archive one entry at a time: open the archive with zip_open(), call zip_read() repeatedly to advance through the entries, then close it with zip_close().
Each successful call returns a zip entry resource — a handle you pass to the zip_entry_* functions to inspect or read that entry. When there are no more entries, zip_read() returns false, which is what terminates the loop. On failure it returns the number of an error code instead.
Syntax
The syntax of the zip_read() function is as follows:
Syntax of the zip_read() function in PHP
resource|int|false zip_read(resource $zip)$zip is the archive resource returned by zip_open(). The return value is one of:
- a zip entry resource — there is an entry to process;
false— the end of the archive has been reached;- an integer error code — something went wrong reading the archive.
Why iterate with zip_read()?
The procedural API is a forward-only cursor: there is no "list all entries" call. You read one entry, do something with it (get its name and sizes, or extract its contents), then ask for the next. This keeps memory use low because only one entry is in scope at a time, but it also means you cannot jump to an arbitrary entry — you have to walk from the start.
Usage Examples
Example: Listing the Entries in a Zip Archive
This loop opens an archive and prints metadata for every entry it contains:
Reading the Entries in a Zip Archive in PHP
$zip = zip_open("example.zip");
if (!is_resource($zip)) {
throw new RuntimeException("Failed to open zip archive (error code: $zip)");
}
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Uncompressed Size: " . zip_entry_filesize($zip_entry) . "\n";
}
zip_close($zip);The code opens example.zip with zip_open(), then loops while zip_read() keeps handing back entry resources. For each entry it prints the name with zip_entry_name(), the stored size with zip_entry_compressedsize(), and the original size with zip_entry_filesize(). Finally zip_close() releases the archive.
Example: Reading an Entry's Contents
zip_read() only positions you at an entry; to read the actual bytes you must open the entry with zip_entry_open() and pull data with zip_entry_read():
$zip = zip_open("example.zip");
if (is_resource($zip)) {
while ($entry = zip_read($zip)) {
if (zip_entry_open($zip, $entry, "r")) {
$contents = zip_entry_read($entry, zip_entry_filesize($entry));
echo zip_entry_name($entry) . ":\n" . $contents . "\n";
zip_entry_close($entry);
}
}
zip_close($zip);
}Common Gotchas
- Truthiness of the loop.
while ($entry = zip_read($zip))works because a valid resource is truthy andfalseends the loop. But an integer error code is also returned on failure — checkis_resource($zip)afterzip_open()so you never iterate over a broken handle. - You can only read forward. There is no rewind. To process the archive again, reopen it with
zip_open(). - Removed in PHP 8. These functions were deleted in PHP 8.0. Code that must run on modern PHP should use the object-oriented
ZipArchiveclass instead.
Modern Alternative: ZipArchive
On PHP 8 and later, iterate an archive with the ZipArchive class. It also gives you random access by index, which zip_read() cannot:
$zip = new ZipArchive();
if ($zip->open("example.zip") === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
echo "Name: {$stat['name']}\n";
echo "Compressed Size: {$stat['comp_size']}\n";
echo "Uncompressed Size: {$stat['size']}\n";
}
$zip->close();
}See the PHP Zip extension overview for the full ZipArchive API.
Conclusion
zip_read() advances a forward-only cursor through the entries of a zip archive opened with zip_open(), returning a zip entry resource each time and false at the end. It is the heart of the legacy procedural zip-reading loop, paired with the zip_entry_* functions for inspecting and extracting each entry. Because this API was removed in PHP 8.0, prefer ZipArchive for any code that targets current PHP versions.