Thursday, November 20, 2025
HomeLanguagesPHP ZipArchive count() Function

PHP ZipArchive count() Function

The ZipArchive::count() function is an inbuilt function in PHP that is used to count the number of files in a zip archive.

Syntax:

int ZipArchive::count()

Parameters: This function does not accept any parameters.

Return Value: This function returns the number of files in the zip archive.

Example 1: The following code demonstrates the total count of a zip 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 'Total files/directory: ' . $zip->count();
             
        // 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 shows the total number of files after adding the extra 3 files as given in the code.

PHP




<?php
 
    // Create a new ZipArchive object
    $zip = new ZipArchive;
 
    // Check for opening the zip file
    if ($zip->open('Geeks.zip', ZipArchive::CREATE)) {
         
        echo 'Total number of files: '
              . $zip->count() . '<br>';
 
        // 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'
        );
 
         
        echo 'Total files after adding new files: '
            . $zip->count();
         
        // 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.count.php

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS