Home PHP Zip zip_entry_open() PHP zip_entry_open() Function Definition In PHP, zip_entry_open() opens a ZIP entry from an archive so that you can read its data with zip_entry_read().
It is deprecated as of PHP 8.0. With ZipArchive, use getFromName() or getFromIndex() to read an entry as a string, or getStream() when you want a stream.
Syntax Call zip_entry_open() with the values you need. Start with this line.
// Deprecated (PHP 8.0+)
$zip = zip_open('archive.zip');
$entry = zip_read($zip);
zip_entry_open($zip, $entry, 'rb');
// Modern equivalent
$zip = new ZipArchive();
$zip->open('archive.zip');
$text = $zip->getFromName('hello.txt'); Call What it does zip_entry_open($zip, $entry)Deprecated: opens the entry for reading with the default mode rb. @zip_entry_open($zip, $entry, 'rb')Deprecated: the @ operator hides the E_DEPRECATED notice only. $zip->getFromName('hello.txt')Modern ZipArchive: reads the whole entry into a string, or returns false if it is missing.
zip_entry_open() is deprecated in PHP 8.0. ZipArchive has no separate open step for entries: getFromName(), getFromIndex(), and getStream() read the data directly.
Library declarations zip_entry_open(resource $zip, resource $zip_entry, string $mode = "rb"): bool Parameters Parameter Description zip Required. The ZIP resource returned by zip_open(). zip_entry Required. A directory entry resource returned by zip_read(). mode Optional. The open mode. The default is "rb" (read binary). It is the only practical mode, because this old API is read-only.
Return Value Return Value Description true The entry was opened and is ready for zip_entry_read(). false The entry could not be opened.
PHP Examples 1. Read an Entry by Name 2. Opening a Missing Entry 3. Find an Index, Then Read It Example 1 — Read an Entry by Name Open a temp archive and read hello.txt into a string. The legacy code is a comment.
<?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):
// zip_entry_open($zip, $entry, 'rb');
// $text = zip_entry_read($entry, zip_entry_filesize($entry));
$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);
$text = $zip->getFromName('hello.txt');
echo 'Opened and read: ', trim($text), PHP_EOL;
$zip->close();
unlink($path); Opened and read: Hello zip How It Works Create the demo archive and open it with ZipArchive. Call getFromName() with the entry name. Trim the trailing newline from the returned text. Print the text, then close and delete the archive. Example 2 — Opening a Missing Entry Both getFromName() and getStream() return false when the entry does not exist.
<?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);
$text = $zip->getFromName('nope.txt');
if ($text === false) {
echo 'nope.txt: cannot open', PHP_EOL;
}
$stream = $zip->getStream('nope.txt');
echo 'Stream is false: ', $stream === false ? 'yes' : 'no', PHP_EOL;
$zip->close();
unlink($path); nope.txt: cannot open
Stream is false: yes How It Works Open the demo archive. Ask getFromName() for an entry that is not there. Compare the result with false to detect the failure. Do the same check with getStream(), then clean up. Frequently Asked Questions Is zip_entry_open() deprecated? Yes. The procedural zip_* functions are deprecated as of PHP 8.0. Use ZipArchive::getFromName(), getFromIndex(), or getStream().
Do I have to open an entry with ZipArchive? No separate open step is needed. The get methods read the entry in one call.
What is the difference between getFromName() and getStream()? getFromName() returns the whole entry as a string. getStream() returns a stream so you can read in chunks, which is better for big files.
What does zip_entry_open() return on failure? It returns false. The ZipArchive methods also return false when the entry cannot be found.
Can I write to an entry with zip_entry_open()? No. The old API only reads archives. Use ZipArchive::addFromString() or addFile() to write.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion