zip_entry_open()
The zip_entry_open() function is a built-in function in PHP that is used to open a file in a zip archive. The function returns a handle that can be used to read
⚠️ Deprecation Notice: The zip_entry_open() function belongs to the legacy zip extension, which was deprecated in PHP 7.4 and removed in PHP 8.0. The examples below are provided for historical reference only. For modern PHP applications, use the ZipArchive class instead.
zip_entry_open() prepares a single entry (file) inside an opened zip archive so that its contents can be read. It works hand in hand with zip_open(), zip_read(), and zip_entry_read(): you open the archive, iterate to an entry, open that entry, then read from it. Without calling zip_entry_open() first, a call to zip_entry_read() has nothing to read from.
This page covers the function's signature, its parameters and return value, a complete read-an-entry example, and — most importantly — the modern ZipArchive replacement you should use today.
Syntax
resource zip_entry_open(resource $zip, resource $zip_entry, string $mode = "r")Parameters
| Parameter | Description |
|---|---|
$zip | The archive handle returned by zip_open(). |
$zip_entry | An entry handle returned by zip_read(). |
$mode | The access mode. The legacy zip extension only ever supported read access, so this is effectively always "r". |
The fourth
$passwordargument seen in some old references was never functional in the bundledzipextension; it is ignored.
Return value
Returns true on success or false on failure. Despite the historical resource type hint, you do not get a new handle back — you keep passing the same $zip_entry handle to zip_entry_read().
Usage Example
Example: Reading a file from a zip archive
Open the archive, walk to an entry, open that entry, read it, and clean up:
$zip = zip_open("example.zip");
if (is_resource($zip)) {
while ($zip_entry = zip_read($zip)) {
// Prepare this entry for reading
if (zip_entry_open($zip, $zip_entry, "r")) {
$name = zip_entry_name($zip_entry);
$contents = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo "$name: $contents\n";
zip_entry_close($zip_entry);
}
}
zip_close($zip);
}Here zip_open() opens the archive, zip_read() advances to each entry in turn, zip_entry_open() makes the current entry readable, and zip_entry_read() returns its bytes. Always pair every successful open with zip_entry_close(), and close the archive with zip_close() when you are done.
Modern Alternative: ZipArchive Class
To achieve the same result in PHP 8.0+, use the built-in ZipArchive class:
$zip = new ZipArchive;
if ($zip->open('example.zip') === true) {
// Loop over every entry by index
for ($i = 0; $i < $zip->numFiles; $i++) {
$name = $zip->getNameIndex($i);
$contents = $zip->getFromIndex($i);
echo "$name: $contents\n";
}
$zip->close();
}ZipArchive needs no separate "open the entry" step: getFromIndex() (or getFromName()) reads an entry's contents directly, and it works in every modern PHP version.
Conclusion
In this article, we've discussed the PHP zip_entry_open() function and how it can be used to open a file in a zip archive. We've explained what the function does, its syntax, and provided an example of how it can be used in a practical scenario. Note that zip_entry_open() is deprecated and removed in PHP 8.0; for modern projects, use the ZipArchive class to open and read files in a zip archive.