Thursday, October 23, 2025
HomeLanguagesPHP ZipArchive close() Function

PHP ZipArchive close() Function

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 "neveropen"
        if($zip->addEmptyDir('neveropen'))
        {
            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('neveropen.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 neveropen'
        );
  
        // 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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS