Home  >  Article  >  Backend Development  >  How to open zip file with PHP? Use of zip_open() function

How to open zip file with PHP? Use of zip_open() function

青灯夜游
青灯夜游Original
2019-02-14 18:07:534696browse

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.

How to open zip file with PHP? Use of zip_open() function

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:

How to open zip file with PHP? Use of zip_open() function

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:

How to open zip file with PHP? Use of zip_open() function

##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 . "文件"); 
?>

Output:

How to open zip file with PHP? Use of zip_open() function

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn