PHP zip_entry_open() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

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.

PHP
// 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');
CallWhat 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
PHP
zip_entry_open(resource $zip, resource $zip_entry, string $mode = "rb"): bool

Parameters

ParameterDescription
zipRequired. The ZIP resource returned by zip_open().
zip_entryRequired. A directory entry resource returned by zip_read().
modeOptional. 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 ValueDescription
trueThe entry was opened and is ready for zip_entry_read().
falseThe entry could not be opened.

PHP Examples

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
<?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);

How It Works

  1. Create the demo archive and open it with ZipArchive.
  2. Call getFromName() with the entry name.
  3. Trim the trailing newline from the returned text.
  4. 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
<?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);

How It Works

  1. Open the demo archive.
  2. Ask getFromName() for an entry that is not there.
  3. Compare the result with false to detect the failure.
  4. Do the same check with getStream(), then clean up.

Example 3 — Find an Index, Then Read It

Use locateName() to get an entry index, then read the entry 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);

$index = $zip->locateName('docs/readme.txt');
echo 'Index: ', $index, PHP_EOL;
echo 'Content: ', trim($zip->getFromIndex($index)), PHP_EOL;

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

How It Works

  1. Create and open the demo archive.
  2. Call locateName() to find docs/readme. txt.
  3. Pass that index to getFromIndex() to read the entry.
  4. Print the index and trimmed content, then clean up.

Frequently Asked Questions

Yes. The procedural zip_* functions are deprecated as of PHP 8.0. Use ZipArchive::getFromName(), getFromIndex(), or getStream().
No separate open step is needed. The get methods read the entry in one call.
getFromName() returns the whole entry as a string. getStream() returns a stream so you can read in chunks, which is better for big files.
It returns false. The ZipArchive methods also return false when the entry cannot be found.
No. The old API only reads archives. Use ZipArchive::addFromString() or addFile() to write.

Next: zip_entry_read()

zip_entry_read() reads up to a given number of bytes from an opened ZIP entry.

zip_entry_read() tutorial →

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