PHP Function Reference

PHP ZipArchive - open() Method



The PHP ZipArchive::open() method is used to open a new or existing zip archive for reading, writing or modifying.

Since libzip 1.6.0, a empty file is not a valid archive any longer.

Syntax

public ZipArchive::open(filename, flags)

Parameters

filename Required. Specify the file name of the ZIP archive to open.
flags Optional. Specify the mode to use to open the archive. It can take the following values:
  • ZipArchive::OVERWRITE - If archive exists, ignore its current contents. In other words, handle it the same way as an empty archive.
  • ZipArchive::CREATE - Create the archive if it does not exist.
  • ZipArchive::RDONLY - Open archive in read only mode.
  • ZipArchive::EXCL - Error if archive already exists.
  • ZipArchive::CHECKCONS - Perform additional consistency checks on the archive, and error if they fail.

Return Value

Returns true on success, or one of the following error codes:

  • ZipArchive::ER_EXISTS - File already exists.
  • ZipArchive::ER_INCONS - Zip archive inconsistent.
  • ZipArchive::ER_INVAL - Invalid argument.
  • ZipArchive::ER_MEMORY - Malloc failure.
  • ZipArchive::ER_NOENT - No such file.
  • ZipArchive::ER_NOZIP - Not a zip archive.
  • ZipArchive::ER_OPEN - Can't open file.
  • ZipArchive::ER_READ - Read error.
  • ZipArchive::ER_SEEK - Seek error.

Example: open and extract

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 open and extract this zip file content at the specified location:

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

if ($result === TRUE) {
  $zip->extractTo('/example/');
  $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: create an archive

The example below describes how to create an archive.

<?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');
  $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.

Example: create a temporary archive

Consider the example below where this method is used to create a temporary archive.

<?php
$name = tempnam(sys_get_temp_dir(), "FOO");
$zip = new ZipArchive;

//truncate as empty file is not valid
$result = $zip->open($name, ZipArchive::OVERWRITE); 

if ($result === TRUE) {
  $zip->addFile('/path/example.pdf', 'newname.pdf');
  $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