In PHP, you can use the built-in function zip_open() to open zip files for reading. The following article will give you a brief introduction to the usage of the zip_open() function. I hope it will be helpful to you.
PHP zip_open() function
zip_open() function creates a new stream and Establish a connection between the stream and the Zip archive. It takes a filename as argument and will return a valid resource handler if the zip file is opened successfully, otherwise an error will be returned. [Video tutorial recommendation: PHP tutorial]
Basic sentence structure:
zip_open( $filename )
Description: Errors that the zip_open() function may return
● If the zip archive is invalid, the zip_open() function will return an ER_OPEN error.
● If the zip archive is empty, the zip_open() function returns an ER_NOZIP error.
Usage example of zip_open() function
Suppose a zip file article.zip contains the following files:
Example 1:
<?php // 打开zip文件 $my_zip = zip_open("article.zip"); if(is_resource($my_zip)) { echo("已成功打开zip文件。"); // 关闭zip文件 zip_close($my_zip); } else echo("无法打开".$my_zip . "文件"); ?>
Output:
##Example 2:<?php // 打开zip文件 $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>"); } // 关闭zip文件 zip_close($my_zip); } else echo("无法打开".$my_zip . "文件"); ?>
The above is the detailed content of How to open zip file with PHP? Use of zip_open() function. For more information, please follow other related articles on the PHP Chinese website!