Saturday, December 13, 2025
HomeLanguagesPHP ZipArchive deleteIndex() Function

PHP ZipArchive deleteIndex() Function

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

Syntax:

bool ZipArchive::deleteIndex(int $index)

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

  • $index: This parameter holds the index number of entries that need to delete.

Return Value: This function returns True on Success and False on Failure.

Example 1: The following code demonstrates the delete() function. It deletes the third file as shown in the output.

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->deleteIndex(2)) {
            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 delete() function with index 2.

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->deleteIndex(2))
      {
          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.deleteindex.php

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32446 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12030 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7200 POSTS0 COMMENTS
Thapelo Manthata
6897 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS