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.
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.
glob( string $pattern, int $flags = 0 )
Program 1: This program uses glob() and count() function to count all files within the directory.
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.
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.
readdir( resource $dir_handle )
Program 2: This program uses openddir() and readdir() function to count all files within the directory.
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.
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.
FilesystemIterator() Function: The FilesystemIterator::__construct() function is used to create a new filesystem iterator from the path.
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.
int iterator_count( $iterator )
Program 4: This program uses FilesystemIterator() and iterator_count() function to count all files within the directory.
- Copy the entire contents of a directory to another directory in PHP
- How to get names of all the subfolders and files present in a directory using PHP?
- How to include() all PHP files from a directory ?
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to display all files in a directory using Node.js ?
- How to keep compiled files in a separate directory ?
- How to see the changes in whole directory/folder containing many sass files ?
- How to copy a file from one directory to another using PHP ?
- PHP | dir() (Get instance of the Directory)
- How to create directory with HTML button and PHP ?
- How to Zip a directory in PHP?
- Why PHP script does not create a directory with 777 permissions ?
- How to open a PDF files in web browser using PHP?
- Deleting all files from a folder using PHP
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- Perl | Accessing a Directory using File Globbing
- How to Get the Current Working Directory in Golang?
- Laravel | Directory Structure
- How to check the given path is file or directory in node.js ?
- Creating a dataframe using Excel files
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.