Open In App

PHP | zip_open() Function

Last Updated : 24 Jul, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The zip_open() function is an inbuilt function in PHP which is used to open a zip archive for reading. The zip_open() function creates a new stream and establishes a connection between the stream and a Zip Archive. The filename is sent as a parameter to the zip_open() function and it returns a valid resource handler if the zip archive file is opened successfully otherwise it returns an Error. Syntax:
zip_open( $filename )
Parameters: This function accepts single parameter $filename which is mandatory. It is used to specify the zip resource to be opened. Return Value: It returns a valid resource handler if the zip archive file is opened successfully otherwise it returns an Error. Errors And Exceptions:
  • The zip_open() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_open() function returns an ER_NOZIP error if the zip archive is empty.
Below programs illustrate the zip_open() function in PHP:
Suppose a zip file article.zip contains the following files: article.zip content.xlsx gfg.pdf image.jpeg
Program 1: php
<?php

// Opening zip file
$my_zip = zip_open("article.zip");

if(is_resource($my_zip))
{ 
    echo("Zip file opened successfully.");
    
    // Closing zip file
    zip_close($my_zip);
} 
else
    echo($my_zip . "file can not be opened");
?>
Output:
Zip file opened successfully.
Program 2: php
<?php

// Opening zip file
$my_zip= zip_open("article.zip");

if(is_resource($my_zip)) 
{ 
    while($zipfiles = zip_read($my_zip)) 
    { 
        $file_name = zip_entry_name($zipfiles);
        echo("File Name: " . $file_name . "<br>");
    } 
    
    // Closing zip file
    zip_close($my_zip);
} 
else
    echo($my_zip . "file Can not be opened");
?>
Output:
File Name: article/article.zip
File Name: article/content.xlsx
File Name: article/gfg.pdf
File Name: article/image.jpeg
Related Articles: Reference: http://php.net/manual/en/function.zip-open.php

Next Article
Practice Tags :

Similar Reads