PHP zip_close() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_close() closes a ZIP archive that was opened with the old procedural zip_open() function.

It is deprecated as of PHP 8.0. For new code, use the ZipArchive class and call $zip->close(), which also saves any changes you made while the archive was open.

Syntax

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

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

// Modern equivalent
$zip = new ZipArchive();
$zip->open('archive.zip');
$zip->close();
CallWhat it does
zip_close($zip)Deprecated: closes the resource returned by zip_open() and frees it.
@zip_close($zip)Deprecated: the @ operator hides the E_DEPRECATED notice, but the function is still old.
$zip->close()Modern ZipArchive: closes the archive, writes pending changes, and returns true or false.

zip_close() is deprecated in PHP 8.0. It only closes read handles from zip_open(). ZipArchive::close() can both read and write archives, so it is the one to learn first.

Library declarations
PHP
zip_close(resource $zip): void

Parameters

ParameterDescription
zipRequired. The ZIP resource returned by zip_open(). In ZipArchive you call close() on the object instead.

Return Value

Return ValueDescription
voidzip_close() returns nothing. ZipArchive::close() returns true on success and false on failure.

PHP Examples

Example 1 — Close a ZipArchive After Writing

Create a small archive in the temp folder, close it, reopen it, and clean up. The old zip_close() call is shown as a comment.

PHP
<?php
// Legacy (deprecated since PHP 8.0):
// $zip = zip_open($path);
// zip_close($zip);

$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('hello.txt', "Hello zip\n");
echo 'Closed: ', $zip->close() ? 'yes' : 'no', PHP_EOL;

$zip = new ZipArchive();
$zip->open($path);
echo 'Entries after reopen: ', $zip->numFiles, PHP_EOL;
$zip->close();

unlink($path);
echo 'Cleaned up: ', file_exists($path) ? 'no' : 'yes', PHP_EOL;

How It Works

  1. Build a unique temp path for the demo archive.
  2. Open it with CREATE, add one entry, and call close() to save it.
  3. Reopen the archive and print how many entries it holds.
  4. Close it again and unlink the temp file.

Example 2 — Empty Archives Are Not Written

Closing an archive that has no entries does not leave a file on disk. Compare it with an archive that has one entry.

PHP
<?php
$empty = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_empty_' . uniqid() . '.zip';
$full = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_full_' . uniqid() . '.zip';

$a = new ZipArchive();
$a->open($empty, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$a->close();

$b = new ZipArchive();
$b->open($full, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$b->addFromString('note.txt', 'hi');
$b->close();

echo 'Empty archive file exists: ', file_exists($empty) ? 'yes' : 'no', PHP_EOL;
echo 'Non-empty archive file exists: ', file_exists($full) ? 'yes' : 'no', PHP_EOL;

if (file_exists($empty)) {
    unlink($empty);
}
unlink($full);

How It Works

  1. Open one archive and close it without adding anything.
  2. Open a second archive, add note. txt, and close it.
  3. Check which of the two files exists on disk.
  4. Remove the temp files so the script leaves nothing behind.

Example 3 — Always Close With finally

Wrap your work in try/finally so close() runs even if something throws. The helper returns how many entries were added.

PHP
<?php
function buildArchive(string $path, array $files): int
{
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        return -1;
    }
    try {
        foreach ($files as $name => $text) {
            $zip->addFromString($name, $text);
        }
        $count = $zip->numFiles;
    } finally {
        $zip->close();
    }
    return $count;
}

$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
$count = buildArchive($path, ['a.txt' => 'A', 'b.txt' => 'B', 'c.txt' => 'C']);

echo 'Entries added: ', $count, PHP_EOL;
echo 'File has data: ', filesize($path) > 0 ? 'yes' : 'no', PHP_EOL;
unlink($path);

How It Works

  1. Write buildArchive() that opens a ZipArchive and checks the open result.
  2. Add every file inside try and read numFiles.
  3. Call close() in finally so it always runs.
  4. Print the count, check the file size, and unlink the temp archive.

Frequently Asked Questions

Yes. The procedural zip_* functions are deprecated as of PHP 8.0. Use ZipArchive and its close() method instead.
Call $zip->close() on your ZipArchive object. It returns true on success and also writes any added or changed entries to disk.
PHP closes it when the object is destroyed, but relying on that is risky. Call close() yourself so you can check the result and be sure changes are saved.
No, it returns void. ZipArchive::close() returns a bool, which is handy for error checks.
ZipArchive does not write archives that contain zero entries. Add at least one file or folder before calling close().

Next: zip_entry_close()

zip_entry_close() closes a single entry that was opened with the old procedural zip_entry_.

zip_entry_close() 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