BBM Tutor
BBM Tutor

PHP Zip - zip_close()

Description & Uses of PHP zip_close()

The PHP zip_close() function causes the stream to be closed and the connection to the corresponding Zip Archive to be broken.

Simple Example - PHP zip_close(Resource $ZipHandler);

PHP Input Screen

<?php
$ZipHandler = zip_open("./YGen.zip");
if(is_resource($ZipHandler)) {
echo("Successfully Zip Archive File Opened.");
/* Zip Archive's Resource Handler Closing Operation */
zip_close($ZipHandler);
} else {
echo("Failed to Open. Error Code: " . $ZipHandler);
}
?>

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_close(Resource $ZipHandler);
?>

Parameter & Return Type

Description

Resource $ZipHandler
Specifies the Resource Handler of an already opened Zip Archive File. (*Required)
Return Void
Returns NULL.

What is PHP zip_close() ?

  • The connection between the stream and the Zip Archive File is cancelled.
  • The Zip Archive must be opened first by using the PHP zip_open() function only if Success otherwise the PHP zip_close() function produce the PHP Warning.
  • Resource Handler of a Stream for the Zip Archive File must be closed to avoid unwanted (or) unexpected changes during the program execution.
  • It's is a good programming practice to clear the used buffer memory allocated by PHP zip_open() function.
  • 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)
  • Closing of an Invalid or an Empty Resource Handler leads to PHP Warning (Refer Example #2 & #4). It can be avoided by a special Error Handling Technique (Refer Example #3 & #5)
BBM Invoice

Example #1 - PHP zip_close(Resource $ZipHandler);

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 Archive's Resource Handler Closing Operation */
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_close(Resource $ZipHandler);

PHP Input Screen

<?php
/* External Link Representation - Invalid Link */
$ZipHandler = zip_open("https://www.bbminfo.com/Tutor/YGen.zip");

/* Zip Archive's Resource Handler Closing Operation */
zip_close($ZipHandler);
?>

PHP Output Screen

PHP Warning: zip_close() expects parameter 1 to be resource, null given in ../Tutor/error.php on line 5

BBM Invoice

Example #3 - PHP zip_close(Resource $ZipHandler);

PHP Input Screen

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

?>

PHP Output Screen

Failed to Open. Error Code: 11

BBM Invoice

Example #4 - PHP zip_close(Resource $ZipHandler);

PHP Input Screen

<?php
/* External Link Representation - Invalid Link */
$ZipHandler = zip_open("./EmptyBox.zip");

/* Zip Archive's Resource Handler Closing Operation */
zip_close($ZipHandler);
?>

PHP Output Screen

PHP Warning: zip_close() expects parameter 1 to be resource, integer given in ../Tutor/error.php on line 5

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

Example #5 - 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 Archive's Resource Handler Closing Operation */
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.