Tuesday, September 16, 2025
HomeLanguagesPHP ZipArchive deleteName() Function

PHP ZipArchive deleteName() Function

The ZipArchive::deleteName() function is an inbuilt function in PHP that is used to delete an entry from the zip archive using its name.

Syntax:

bool ZipArchive::deleteName(string $name)

Parameters: This function accepts a single parameter that is described below.

  • $name: This parameter holds the file name that you want to delete from the zip archive.

Return Value: This function returns “true” on success and “false” on failure.

Example 1: The following code demonstrates the deleteName() function.

PHP




<?php
 
    // Create a new ZipArchive object
    $zip = new ZipArchive;
 
   // Check for opening the zip file
   if ($zip->open('Geeks.zip', ZipArchive::CREATE))
   {
     
      if($zip->deleteName('GFG2.txt')) {
          echo 'File deleted successfully';
      } else {
          echo 'File not deleted';
      }
     
      // Close the zip file
     $zip->close();
  }
  // If zip file is not open/exist
 else
  {
     echo 'Failed to open zip file';
  }
?>


Output:

 

Example 2: The following code demonstrates the deleteName() function with the following files.

PHP




<?php
 
    // Create a new ZipArchive object
    $zip = new ZipArchive;
 
    // Check for opening the zip file
    if ($zip->open('Geeks.zip', ZipArchive::CREATE)) {
 
        // Create new txt file and
        // add String to the file
        $zip->addFromString(
            'GFG1.txt',
            'Welcome to neveropen'
        );
 
        $zip->addFromString(
            'GFG2.txt',
            'A computer science portal'
        );
 
        $zip->addFromString(
            'GFG3.txt',
            'Welcome to neveropen'
        );
 
        if($zip->deleteName('GFG3.txt')) {
            echo 'File deleted successfully';
        } else {
            echo 'File not deleted';
        }
     
       // Close the zip file
       $zip->close();
  }
  // If zip file is not open/exist
  else
  {
      echo 'Failed to open zip file';
  }
?>


Output:

 

Reference: https://www.php.net/manual/en/ziparchive.deletename.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
32293 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6656 POSTS0 COMMENTS
Nicole Veronica
11826 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11892 POSTS0 COMMENTS
Shaida Kate Naidoo
6773 POSTS0 COMMENTS
Ted Musemwa
7044 POSTS0 COMMENTS
Thapelo Manthata
6729 POSTS0 COMMENTS
Umr Jansen
6739 POSTS0 COMMENTS