zip_open()
The zip_open() function is a built-in function in PHP that is used to open a zip archive file. The function returns a zip archive resource that can be used to
Note:
zip_open()was deprecated in PHP 7.1 and removed in PHP 8.0. This function is provided for legacy compatibility only. For modern PHP applications, use theZipArchiveclass instead.
The zip_open() function is a built-in function in PHP that was used to open a zip archive file. The function returns a zip archive resource that can be used to read and manipulate the contents of the archive.
Syntax
The syntax of the zip_open() function is as follows:
Syntax of the zip_open() function in PHP
resource zip_open(string $filename [, int $flags [, string &$error ]])Where:
$filenameis the path to the zip archive file to open.$flagsis an optional integer of flags that modify how the archive is opened. In practice this parameter is rarely used and is usually passed as0.$erroris an optional reference variable. When opening fails, PHP sets it to a numeric error code so you can find out why the open failed.
On success the function returns a zip archive resource (an opaque handle). On failure it returns either false or, in some builds, an integer error code — so always test the result before using it.
Why was it removed?
zip_open() belongs to the old procedural Zip API (zip_open(), zip_read(), zip_close(), and the zip_entry_*() helpers). This API was deprecated in PHP 7.1 and removed entirely in PHP 8.0. The replacement is the object-oriented ZipArchive class, which is faster, can both read and write archives, and is the version still maintained today.
Usage Examples
Let's compare the legacy approach with the modern one you should actually use.
Example: Opening a Zip Archive (legacy API)
Suppose you have a zip archive file example.zip and want to open it. With the old API you would call zip_open() and check the result:
Opening a Zip Archive File in PHP (PHP < 8.0 only)
$zip = zip_open("example.zip");
// On failure the function returns an integer error code instead of a resource.
if (is_resource($zip)) {
echo "Archive opened successfully.";
zip_close($zip);
} else {
echo "Failed to open archive. Error code: " . $zip;
}This attempts to open example.zip. Because the function returns a resource on success but an integer error code on failure, the safest check is is_resource(). Reading or extracting entries then requires the companion functions zip_read(), zip_entry_open(), and zip_entry_read() — all of which were also removed in PHP 8.0.
Example: The modern equivalent with ZipArchive
On any supported PHP version (7.x and 8.x), do the same thing with the ZipArchive class. ZipArchive::open() returns true on success or an error code on failure:
Opening and reading a zip archive with ZipArchive
$zip = new ZipArchive();
if ($zip->open("example.zip") === true) {
echo "Archive opened. It contains {$zip->numFiles} file(s).\n";
// Read the first entry's name as an example.
if ($zip->numFiles > 0) {
echo "First entry: " . $zip->getNameIndex(0) . "\n";
}
$zip->close();
} else {
echo "Failed to open archive.\n";
}ZipArchive keeps the open, inspect, and close steps in one well-documented object, and unlike the legacy API it can also create and modify archives with addFile() and addFromString().
Conclusion
In this article, we've discussed the legacy zip_open() function — its syntax, its $flags and $error parameters, its return values, and how to check the result with is_resource(). Because zip_open() was removed in PHP 8.0, treat it as read-only historical reference: modern applications should use the ZipArchive class for reliable, secure archive management. See also zip_read() and zip_close() for the rest of the legacy API.