Friday, September 19, 2025
HomeLanguagesPHP ZipArchive::addEmptyDir() Function

PHP ZipArchive::addEmptyDir() Function

The ZipArchive::addEmptyDir() function is an inbuilt function in PHP which is used to add a new directory.

Syntax:

bool ZipArchive::addEmptyDir( string $dirname , int $flags = 0 )

Parameters: This method accepts two parameters as mentioned above and described below:

  • $dirname: It specifies the directory name to add.
  • $flags: It specifies the Bitmask consisting of ZipArchive::FL_ENC_GUESS, ZipArchive::FL_ENC_UTF_8, ZipArchive::FL_ENC_CP437.

Return Value: This function returns true on success or false on failure. 

Example 1:

PHP




<?php
 
// Create an object of ZipArchive class
$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:

Added an empty directory

Example 2:

PHP




<?php
 
// Create an object of ZipArchive class
$zip = new ZipArchive;
 
// Check for opening the zip file
if ($zip->open('ide.zip')) {
     
    // If zip file is open then add an
    // empty directory "neveropen"
    $zip->addEmptyDir('neveropen');
     
    // Close the zip file
    $zip->close();
}
 
// If zip file is not open/exist
else {
    echo 'Failed to open zip file';
}
?>


Output: Directory added into the zip file. You can open the zip file to check the added directory list.

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

RELATED ARTICLES

Most Popular

Dominic
32303 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11841 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7059 POSTS0 COMMENTS
Thapelo Manthata
6740 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS