PHP | zip_read() Function
The zip_read() function is an inbuilt function in PHP which is used to read an entity present in the opened zip archive. The zip resource is to be read and sent as parameters to the zip_read() function and it returns a resource containing file within the zip archive on success, or FALSE if there is no more entry to read.
Syntax:
zip_read( $zip )
Parameters: This function accepts single parameter $zip which is mandatory. It is used to specify the zip entry resource.
Return Value: It returns a resource containing file within the zip archive on success, or FALSE if there is no entry to read.
Errors And Exceptions:
- The zip_read() function returns an ER_OPEN error if the zip archive is invalid.
- The zip_read() function returns an ER_NOZIP error if the zip archive is empty.
Below programs illustrate the zip_read() function in PHP:
Program 1:
Suppose a zip file article.zip contains the following files:
article.zip
content.xlsx
gfg.pdf
image.jpeg
<?php // Opening a zip archive $zip_handle = zip_open("article.zip"); // Reading a zip file while($zip_entry = zip_read($zip_handle)) { $file = zip_entry_name($zip_entry); echo("File Name: " . $file . "<br>"); } // Close the opend zip file zip_close($zip_handle); ?> |
Output:
File Name: article/article.zip File Name: article/content.xlsx File Name: article/gfg.pdf File Name: article/image.jpeg
Program 2:
Suppose a zip file article.zip contains the following files and directory:
Directory: img
- geeksforgeeks.png
- geeksforgeeks1.png
content.xlsx
gfg.pdf
image.jpeg
<?php // Opening a zip file $zip_handle = zip_open("article.zip"); if(is_resource($zip_handle)) { // Reading a zip entry file while($zip_entry = zip_read($zip_handle)) { $file = zip_entry_name($zip_entry); echo("File Name: " . $file . "<br>"); } // Close the opened xop file zip_close($zip_handle); } else echo("Zip Archive cannot be opened."); ?> |
Output:
File Name: article/content.xlsx File Name: article/gfg.pdf File Name: article/image.jpeg File Name: article/img/ File Name: article/img/geeksforgeeks.png File Name: article/img/geeksforgeeks1.png
Related Articles:
- PHP | zip_entry_close() Function
- PHP | zip_close( ) Function
- PHP | zip_entry_compressedsize() Function
Reference: http://php.net/manual/en/function.zip-read.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 ?
- PHP | Ds\Set first() Function
- D3.js | d3.map.has() Function
- D3.js | d3.map.set() Function
- PHP | exp() Function
- p5.js | tan() function
- PHP | Ds\Set last() Function
- p5.js | hue() function
- p5.js | day() function
- PHP | pow( ) Function
- PHP | Ds\Map last() Function
- p5.js | value() Function
- p5.js | pow() 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.



