How to count files in a directory using PHP? - GeeksforGeeks

07 Jan, 2022
PHP contains many functions like count(), iterator_count(), glob(), openddir(), readdir(), scandir() and FilesystemIterator() to count number of files in a directory.count() Function: The count() functions is an array function which is used to count all elements in an array or something in an object. This function uses COUNT_RECURSIVE as a mode to recursively count the array that is useful for counting all the elements of a multidimensional array.Syntax: 
int count( mixed $array_or_countable, int $mode = COUNT_NORMAL )
glob() Function: The glob() function is a Filesystem function that searches all possible pathnames matching pattern according to the rules used by the libc glob() function.Syntax:  
glob( string $pattern, int $flags = 0 )
Program 1: This program uses glob() and count() function to count all files within the directory.  
php
<?php // Set the current working directory$directory = getcwd()."/"; // Initialize filecount variable$filecount = 0; $files2 = glob( $directory ."*" ); if( $files2 ) {    $filecount = count($files2);} echo $filecount . "files "; ?>
Output: 
20 files
openddir() Function: The openddir() function is used to open a directory handle. The path of the directory to be opened is sent as a parameter to the opendir() function and it returns a directory handle resource on success, or FALSE on failure.Syntax:  
opendir( string $path, resource $context )
readdir() Function: The readdir() function is a directory function which reads the entries from directory handle those are returned in the order in which they are stored by the filesystem.Syntax:  
readdir( resource $dir_handle )
Program 2: This program uses openddir() and readdir() function to count all files within the directory. 
php
<?php // Set the current working directory$dir = getcwd(); // Initialize the counter variable to 0$i = 0; if( $handle = opendir($dir) ) {         while( ($file = readdir($handle)) !== false ) {        if( !in_array($file, array('.', '..')) && !is_dir($dir.$file))            $i++;    }} // Display resultecho "$i files"; ?>
Output: 
20 files
scandir() Function: The scandir() function is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.Syntax:  
scandir( string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, resource $context )
Program 3: This program uses scandir() and count() function to count all files within the directory. 
php
<?php // Set the current working directory$directory = getcwd()."/";  // Returns array of files$files1 = scandir($directory);  // Count number of files and store them to variable$num_files = count($files1) - 2; echo $num_files . " files"; ?>
Output: 
20 files
FilesystemIterator() Function: The FilesystemIterator::__construct() function is used to create a new filesystem iterator from the path.Syntax:  
FilesystemIterator::__construct( string $path, 
      int $flags = FilesystemIterator::KEY_AS_PATHNAME 
      | FilesystemIterator::CURRENT_AS_FILEINFO 
      | FilesystemIterator::SKIP_DOTS )
iterator_count() Function: The iterator_count() function is SPL is used to count the iterator elements.Syntax:  
int iterator_count( $iterator )
Program 4: This program uses FilesystemIterator() and iterator_count() function to count all files within the directory.
php
<?php $fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS); printf("%d files", iterator_count($fi)); ?>
Output:  
20 files 
sagar0719kumar
Picked
PHP
PHP Programs
Web Technologies
Web technologies Questions
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to receive JSON POST with PHP ?
How to pass JavaScript variables to PHP ?
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
How to pass JavaScript variables to PHP ?
Split a comma delimited string into an array in PHP