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 copy a file from one directory to another using PHP ?
The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bool
2 min read
How to Zip a directory in PHP?
ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed. The PHP ZipArchive class can be used to zipping and unzipping. It might be need to install the class if it is not present. Installation for Li
2 min read
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 post data using file_get_contents in PHP ?
The file_get_contents() function in PHP is used to read the contents of a file and make HTTP requests using GET and get HTTP responses using POST methods. The HTTP POST request can be made using the $context parameter of the file_get_contents() function, which posts the specified data to the URL spe
3 min read
How to execute PHP code using command line?
PHP Installation for Windows Users Follow the steps to install PHP on the Windows operating system. Step 1: First, we have to download PHP from its official website. We have to download the .zip file from the respective section depending upon our system architecture(x86 or x64).Step 2: Extract the .
2 min read
How to Convert File Content to Byte Array in PHP ?
Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility t
5 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