PHP Function Reference

PHP zip_open() Function



The PHP zip_open() function is used to open a zip archive for reading.

Note: This function has been DEPRECATED as of PHP 8.0.0. The procedural API is deprecated and ZipArchive should be used instead.

Syntax

zip_open(filename)

Parameters

filename Required. Specify the file name of the ZIP archive to open.

Return Value

Returns a resource handle for later use with zip_read() and zip_close() or returns either false or the number of error if filename does not exist or in case of other error.

Example: open and close an archive

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 close this zip file:

<?php
//opening the zip file
$zip = zip_open("example.zip");
  
if(is_resource($zip)) { 
  echo 'Zip file opened successfully.';
      
  //closing the zip file
  zip_close($zip);
} else {
  echo 'Opening of the Zip file failed.';
}
?>

The output of the above code will be:

Zip file opened successfully.

Example: getting all file names of an archive

The example below describes how to get all file names of a given archive.

<?php
//opening the zip file
$zip = zip_open("example.zip");
  
if(is_resource($zip)) { 
  while($zipfiles = zip_read($zip)) { 
    $file_name = zip_entry_name($zipfiles);
    echo("File Name: $file_name \n");
  } 
      
  //closing the zip file
  zip_close($zip);
} else {
  echo 'Opening of the Zip file failed.';
}
?>

The output of the above code will be:

File Name: example/test.txt
File Name: example/example.csv
File Name: example/image.png

❮ PHP Zip Reference