W3docs

zip_close()

The zip_close() function is a built-in function in PHP that is used to close a zip archive handle. When you are done working with a zip archive, you should

The zip_close() function closes a zip archive that was previously opened with zip_open(). Closing the handle frees the underlying resource and ensures any pending changes are written to the archive. You should always call it once you are finished reading from an archive.

⚠️ Deprecated in modern PHP: The procedural zip_* functions were deprecated in PHP 7.4 and removed in PHP 8.0. The examples below apply to legacy PHP (7.3 and earlier). For current projects, use the object-oriented ZipArchive class.

Syntax

The syntax of the zip_close() function is as follows:

zip_close(resource $zip): void

Where $zip is the zip archive handle returned by zip_open(). The resource type is legacy and only applies to PHP 7.3 and below.

Return Value: zip_close() does not return a meaningful value.

Usage Examples

Let's look at a practical example of using zip_close() in PHP.

Example: Closing a Zip Archive Handle

After opening an archive with zip_open() and reading its entries, close the handle once you are done:

// Legacy PHP 7.3 and below only
$zip = zip_open("example.zip");

if ($zip !== false) {
    // do something with the zip archive
    zip_close($zip);
}

This code opens example.zip with zip_open(). The if ($zip !== false) check guards against a failed open (which returns false or an error code). Once the work is done, zip_close() releases the handle.

Modern Alternative: ZipArchive

For PHP 8.0+, use the ZipArchive class instead:

$zip = new ZipArchive();
if ($zip->open('example.zip') === true) {
    // work with the archive
    $zip->close();
}

Conclusion

The zip_close() function closes a zip archive handle opened with zip_open(). Because the procedural zip_* functions were removed in PHP 8.0, treat zip_close() as legacy and reach for the ZipArchive class in modern code. See also zip_open() and zip_read().

Practice

Practice
What is true about the ZipArchive::close() function in PHP according to the information provided at w3docs.com?
What is true about the ZipArchive::close() function in PHP according to the information provided at w3docs.com?
Was this page helpful?