How to count files in a directory using PHP?
Last Updated :
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 result
echo "$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
Similar Reads
How to delete a file using PHP ? To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context. Syntax: unlink( $filename,
2 min read
How to include() all PHP files from a directory ? Given list of PHP files in a directory and the task is to include all files from a directory. In order to include all PHP files from directory at once need a foreach-loop. Example: This example contains four PHP files (file1.php, file2.php, file3.php, file4.php) in a directory. Create one file named
1 min read
How to count all array elements in PHP ? We have given an array containing some array elements and the task is to count all elements of an array arr using PHP. In order to do this task, we have the following methods in PHP:Table of ContentUsing count() MethodUsing sizeof() MethodUsing a loopUsing iterator_count with ArrayIteratorUsing arra
4 min read
How to get names of all the subfolders and files present in a directory using PHP? Given the path of the folder and the task is to print the names of subfolders and files present inside them. Explanation: In our PHP code, initially, it is checked whether provided path or filename is a directory or not using the is_dir() function. Now, we open the directory using opendir() function
2 min read
How to get the current file name using PHP script ? In this article, we will learn how to get the current filename using PHP. Input : c:/xampp/htdocs/project/home.php Output : project/home.php Input : c:/xampp/htdocs/project/abc.txt Output : project/abc.txt Sometimes, we need to get the current file name with the directory name in which our page is s
2 min read
How to Count of Repeating Digits in a Given Number in PHP ? Counting the number of repeating digits in a given number is a common task in programming. This can be useful in various scenarios, such as data analysis or validation. In this article, we will explore different approaches to counting the repeating digits in a given number using PHP.Table of Content
5 min read