Open In App

PHP | zip_entry_name() Function

Last Updated : 25 Jul, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
The zip_entry_name() function is an inbuilt function in PHP which is used to return the name of a zip archive entry. The zip entry resource is to be read and sent as a parameter to the zip_entry_name() function and it returns the name of the zip entry archive on Success. Syntax:
string zip_entry_name( $zip_entry )
Parameters: This function accepts single parameter $zip_entry which is mandatory. It is used to specify the zip entry resource. Return Value: It returns the name of a zip archive entry on Success. Errors And Exceptions:
  • The zip_entry_name() returns the name of a zip entry archive only on Success otherwise it returns a PHP warning.
  • The zip_entry_name() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_entry_name() function returns an ER_NOZIP error if the zip archive is empty.
Below programs illustrate the zip_entry_name() function in PHP: Program 1:
Suppose a zip file article.zip contains the following file: content.xlsx
php
<?php

// Opening a zip file
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");

// Reading a zip entry archive 
$zip_entry = zip_read($zip_handle); 

// Reading the name of a zip entry archive
$file = zip_entry_name($zip_entry);
echo("File Name: " . $file);

// Closing the zip archive
zip_close($zip_handle);
?>
Output:
File Name: article/content.xlsx
Program 2:
Suppose a zip file article.zip contains following files and directories: Directory: img
  • geeksforgeeks.png
  • geeksforgeeks1.png
content.xlsx gfg.pdf image.jpeg
php
<?php

// Opening a zip file
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");

if(is_resource($zip_handle)) 
{ 
    while($zip_entry = zip_read($zip_handle)) 
    { 
        $file = zip_entry_name($zip_entry);
       
        // Checking the file name of a zip archive entry 
        $file_name = zip_entry_name($zip_entry);
        echo("File Name: " . $file_name . "<br>");
    }
    
    // closing the zip archive
   zip_close($zip_handle);
} 
else
    echo("Zip archive cannot be read.");
?>
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: Reference: http://php.net/manual/en/function.zip-entry-name.php

Next Article
Practice Tags :

Similar Reads