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
