PHP zip_open() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_open() opens a ZIP archive file for reading and returns a resource you can pass to zip_read().

It is deprecated as of PHP 8.0. Use new ZipArchive() and call $zip->open($filename), which can read and write archives and returns true or an error code.

Syntax

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

PHP
// Deprecated (PHP 8.0+)
$zip = zip_open('archive.zip');

// Modern equivalent
$zip = new ZipArchive();
$result = $zip->open('archive.zip');
CallWhat it does
zip_open('archive.zip')Deprecated: opens a ZIP file for reading and returns a resource, or an error value.
@zip_open('archive.zip')Deprecated: the @ operator hides the E_DEPRECATED notice only.
$zip->open('archive.zip', ZipArchive::CREATE)Modern ZipArchive: opens the file, or creates it when it does not exist. Returns true or an error code.

zip_open() is deprecated in PHP 8.0. Its error handling was awkward (a resource, an integer, or false). ZipArchive::open() returns true on success, or an integer error code such as ZipArchive::ER_NOENT, so always compare with === true.

Library declarations
PHP
zip_open(string $filename): resource|int|false

Parameters

ParameterDescription
filenameRequired. The path of the ZIP file to open. ZipArchive::open() also takes an optional flags argument such as ZipArchive::CREATE.

Return Value

Return ValueDescription
resourceA ZIP resource you can pass to zip_read() and zip_close().
intAn error code, when the file could not be opened.
falseThe archive could not be opened.

PHP Examples

Example 1 — Open and Count Entries

Create a demo archive, open it with ZipArchive, and print how many entries it has.

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 = zip_open($path);

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

echo 'Open result: ', $result === true ? 'ok' : 'error', PHP_EOL;
echo 'Entries: ', $zip->numFiles, PHP_EOL;

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

How It Works

  1. Create the demo archive in the temp folder.
  2. Create a ZipArchive object and call open().
  3. Compare the result with true and print the entry count.
  4. Close the archive and delete the temp file.

Example 2 — Opening a File That Does Not Exist

ZipArchive::open() returns an error code instead of true when the file is missing.

PHP
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_missing_' . uniqid() . '.zip';

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

echo 'Result is true: ', $result === true ? 'yes' : 'no', PHP_EOL;
echo 'Is ER_NOENT: ', $result === ZipArchive::ER_NOENT ? 'yes' : 'no', PHP_EOL;

How It Works

  1. Build a path for a file that was never created.
  2. Call open() without the CREATE flag.
  3. Check that the result is not true.
  4. Compare it with ZipArchive::ER_NOENT to confirm the file was not found.

Example 3 — Friendly Open Messages

Write a helper that turns open() results into short messages, using a real zip, a missing file, and a plain text file.

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;
}

function describeOpen(string $path): string
{
    $zip = new ZipArchive();
    $result = $zip->open($path);
    if ($result === true) {
        $zip->close();
        return 'ok';
    }
    if ($result === ZipArchive::ER_NOENT) {
        return 'file not found';
    }
    if ($result === ZipArchive::ER_NOZIP) {
        return 'not a zip file';
    }
    return 'error code ' . $result;
}

$good = makeDemoZip();
$missing = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_missing_' . uniqid() . '.zip';
$text = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_text_' . uniqid() . '.zip';
file_put_contents($text, 'This is plain text, not a zip file.');

echo 'real zip: ', describeOpen($good), PHP_EOL;
echo 'missing: ', describeOpen($missing), PHP_EOL;
echo 'text file: ', describeOpen($text), PHP_EOL;

unlink($good);
unlink($text);

How It Works

  1. Write describeOpen() that calls ZipArchive::open() and checks the result.
  2. Return ok for true, and use ER_NOENT and ER_NOZIP for the two common errors.
  3. Test it with a real archive, a missing path, and a plain text file.
  4. Print the three messages and unlink the temp files.

Frequently Asked Questions

Yes. The procedural zip_* functions are deprecated as of PHP 8.0. Use new ZipArchive() and its open() method.
It returns true on success. On failure it returns an integer error code, such as ZipArchive::ER_NOENT for a missing file or ZipArchive::ER_NOZIP for a file that is not a ZIP.
Error codes are non-zero integers, which PHP treats as truthy. A plain if ($zip->open($path)) could wrongly pass on an error.
Pass ZipArchive::CREATE (and often ZipArchive::OVERWRITE) as the second argument to open(). The old zip_open() could not create archives.
The ZipArchive object itself. You call methods on it, such as getNameIndex(), getFromName(), and close().

Next: zip_read()

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

zip_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