PHP | zip_open() Function
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 // 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 // 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:
- PHP | zip_close( ) Function
- PHP | zip_entry_compressionmethod() Function
- PHP | zip_entry_close() Function
Reference: http://php.net/manual/en/function.zip-open.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- p5.js | second() function
- D3.js | d3.map.get() Function
- p5.js | sq() function
- p5.js | abs() function
- p5.js | arc() Function
- p5.js | nf() Function
- p5.js | pow() function
- p5.js | nfs() Function
- p5.js | nfp() Function
- p5.js | nfc() function
- CSS | url() Function
- D3.js | d3.lab() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



