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');
Call
What 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
Parameter
Description
filename
Required. The path of the ZIP file to open. ZipArchive::open() also takes an optional flags argument such as ZipArchive::CREATE.
Return Value
Return Value
Description
resource
A ZIP resource you can pass to zip_read() and zip_close().
int
An error code, when the file could not be opened.
false
The 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.
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);
Output
real zip: ok
missing: file not found
text file: not a zip file
How It Works
Write describeOpen() that calls ZipArchive::open() and checks the result.
Return ok for true, and use ER_NOENT and ER_NOZIP for the two common errors.
Test it with a real archive, a missing path, and a plain text file.
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().