PHP ZipArchive close() Function
Last Updated :
27 Sep, 2022
The PHP ZipArchive::close() function is an inbuilt function in PHP that is used to close the opened archive file and save the changes. This function is called after performing all operations on the zip file. If the zip archive does not contain any file then by default zip file is deleted.
Syntax:
bool ZipArchive::close()
Parameters: This function does not accept any parameter.
Return Value: This function returns “true” on success and “false” on failure.
Example 1: In this example, we will describe the ZipArchive::close() function. First, we will open the zip archive and add a directory to the file, and add then close the file.
PHP
<?php
$zip = new ZipArchive;
if ( $zip ->open( 'Geeks.zip' )) {
if ( $zip ->addEmptyDir( 'GeeksforGeeks' ))
{
echo 'Added an empty directory' ;
}
else
{
echo 'Directory can not created' ;
}
$zip ->close();
}
else {
echo 'Failed to open zip file' ;
}
?>
|
Output:
Example 2: In this example, we will open the zip archive and add a file with some string content, and after performing the operation, we will use ZipArchive::close() function to close the file.
PHP
<?php
$zip = new ZipArchive;
$file = $zip ->open( 'geeks.zip' , ZipArchive::CREATE);
if ( $file === TRUE) {
$zip ->addFromString(
'GFG.txt' ,
'Welcome to GeeksforGeeks'
);
$zip ->close();
echo 'File Added Successfully.' ;
} else {
echo 'Failed to Adding file.' ;
}
?>
|
Output:
Reference: https://www.php.net/manual/en/ziparchive.close.php
Share your thoughts in the comments
Please Login to comment...