PHP Function Reference

PHP ZipArchive - count() Method



The PHP ZipArchive::count() method is used to count the number of files in the archive.

Syntax

public ZipArchive::count()

Parameters

No parameter is required.

Return Value

Returns the number of files in the archive.

Example: count the number of files

Lets assume that we have a zip file called example.zip which contains the following files:

test.txt
example.csv
image.png

The example below demonstrates how to count the number of files in this given zip file.

<?php
$zip = new ZipArchive;
$result = $zip->open('example.zip');

if ($result === TRUE) {
  $file_num = $zip->count();
  echo "The zip file contains $file_num files." ;
  $zip->close();
} else {
  echo 'Opening of the Zip file failed.';
}
?>

The output of the above code will be:

The zip file contains 3 files.

❮ PHP Zip Reference