Open In App

PHP ZipArchive close() Function

Last Updated : 27 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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
    // Create a new ZipArchive object
    $zip = new ZipArchive;
  
    // Check for opening the zip file
    if ($zip->open('Geeks.zip')) {
          
        // If zip file is open then add an
        // empty directory "GeeksforGeeks"
        if($zip->addEmptyDir('GeeksforGeeks'))
        {
            echo 'Added an empty directory';
        
        else 
        {
            echo 'Directory can not created';
        }
          
        // Close the zip file
        $zip->close();
    }
    // If zip file is not open/exist
    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
    // Create a new ZipArchive class
    $zip = new ZipArchive;
  
    // Open a zip file
    $file = $zip->open('geeks.zip', ZipArchive::CREATE);
  
    // If zip file is open
    if ($file === TRUE) {
  
        // Create new txt file and
        // add String to the file
        $zip->addFromString(
            'GFG.txt'
            'Welcome to GeeksforGeeks'
        );
  
        // Close the opened file
        $zip->close();
        echo 'File Added Successfully.';
    } else {
        echo 'Failed to Adding file.';
    }
?>


Output:

 

Reference: https://www.php.net/manual/en/ziparchive.close.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads