PHP Function Reference

PHP ZipArchive - close() Method



The PHP ZipArchive::close() method is used to close opened or created archive and save changes. This method is automatically called at the end of the script.

Syntax

public ZipArchive::close()

Parameters

No parameter is required.

Return Value

Returns true on success or false on failure.

Example: ZipArchive::close() example

Lets assume that we have a zip file called example.zip which contains the following files:

test.txt
example.csv
image.png

The example below demonstrates how to close this zip file manually after extracting the content at the specified location:

<?php
$zip = new ZipArchive;
$result = $zip->open('example.zip');

if ($result === TRUE) {
  $zip->extractTo('/example/');

  //closing the archive
  $zip->close();
  
  echo 'Zip file opened and extracted successfully.';
} else {
  echo 'Opening of the Zip file failed.';
}
?>

The output of the above code will be:

Zip file opened and extracted successfully.

Example: creating and closing an archive

The example below describes how to close a newly created archive manually.

<?php
$zip = new ZipArchive;
$result = $zip->open('example.zip', ZipArchive::CREATE);

if ($result === TRUE) {
  //adding files to the archive
  $zip->addFromString('test.txt', 'file content goes here');
  $zip->addFile('/path/example.pdf', 'newname.pdf');
  
  //closing the archive
  $zip->close();

  echo 'Zip file created successfully.';
} else {
  echo 'Zip file can not be created.';
}
?>

The output of the above code will be:

Zip file created successfully.

❮ PHP Zip Reference