BBM Tutor
BBM Tutor

PHP Zip - zip_open()

Description & Uses of PHP zip_open()

The PHP zip_open() function is used to open the specified Zip Archive file. It returns a valid Resource handler only if the Zip archive file is opened successfully otherwise an ERROR.

Simple Example - PHP zip_open(String $file_path);

PHP Input Screen

<?php
$ZipHandler = zip_open("YGen.zip");
if(is_resource($ZipHandler)) {
echo("Successfully Zip Archive File Opened.");
} else {
echo("Failed to Open.");
}
?>

PHP Output Screen

Successfully Zip Archive File Opened.

bbminfo Demo Zip Archive File: YGen.zip

  • It contains 3 Files, they are
    • YGen.jpg
    • YGen.pdf
    • YGen.txt
  • Download the Demo Zip Archive File: YGen.zip for your tutorial reference purpose.
BBM Invoice

Simple PHP Syntax

<?php
zip_open(String $file_path);
?>

Parameter & Return Type

Description

String $file_path
The File Path of the specified zip archive file to be open. (*Required)
Return Mixed $ZipHandler
Returns a resource handler if Success otherwise an error.

What is PHP zip_open() ?

  • Opening a Zip Archive with the zip_open function creates a new stream and establishes a connection between the stream and a Zip Archive.
  • It Returns a Resource Handler for the newly created stream only if the Zip Archive File is a Valid Link otherwise an error code.
  • Common Error Codes in PHP Zip Functions
    • Invalide Zip Archive - Returns an ER_OPEN (Error Code: 11)
    • Empty Zip Archive - Returns an ER_NOZIP (Error Code: 19)
  • Its only accept the Internal Link Representation of a Specifed Zip archive file like (./YGen.zip) otherwise it returns an Error Code : 11 on an external Link Representation of an above mentioned Zip archive file like (https://www.bbminfo.com/Tutor/YGen.zip) or an invalid File path. (Refer Example #2)
  • Its a Case Sensitive. (Refer Example #3)
  • The opened zip file resource handler can be used later with the PHP zip_read() function and PHP zip_close() function. (Refer Example #1)
BBM Invoice

Example #1 - PHP zip_open(String $file_path);

PHP Input Screen

<?php
$ZipHandler = zip_open("./YGen.zip");
if(is_resource($ZipHandler)) {
while($zipEntry = zip_read($ZipHandler)) {
$file_Name = zip_entry_name($zipEntry);
echo("File Name: " . $file_Name);
echo "<br>" ;
}
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

File Name: YGen.jpg
File Name: YGen.pdf
File Name: YGen.txt

BBM Invoice

Example #2 - PHP zip_open(String $file_path);

PHP Input Screen

<?php
/* External Link Representation */
$ZipHandler = zip_open("https://www.bbminfo.com/Tutor/YGen.zip");
if(is_resource($ZipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

Failed to Open. Error Code: 11

BBM Invoice

Example #3 - PHP zip_open(String $file_path);

PHP Input Screen

<?php
/* Case Sensitive Mistake: Ygen.zip instead of YGen.zip */
$ZipHandler = zip_open("./Ygen.zip");
if(is_resource($ZipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

Failed to Open. Error Code: 11

Example #4 - PHP zip_open(String $file_path);

PHP Input Screen

<?php
/* Empty Zip FIle - 0 Files in the Zip FIle */
$ZipHandler = zip_open("./EmptyBox.zip");
if(is_resource($ZipHandler)) {
echo("Successfully Zip Archive File Opened.");
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

PHP Output Screen

Failed to Open. Error Code: 19

bbminfo Demo Zip Archive File: EmptyBox.zip

  • It contains 0 Files (No Files are there in the Zip Archive)
  • Download the Demo Zip Archive File: EmptyBox.zip for your tutorial reference purpose.
BBM Invoice
 
Disclaimer: Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.