Change language

How to read files in a directory using PHP?

| |
count() Function :count() - it is an array function that is used to count all the elements in an array or anything in an object. This function uses COUNT_RECURSIVE as the mode for recursive array counting, which is useful for counting all elements of a multidimensional array.Syntax:
 int count (mixed $array_or_countable, int $mode = COUNT_NORMAL)
glob() function:glob() function - it is a filesystem function that looks for all possible paths that match a 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() to count all files in a directory.  
// Set current working directory $directory = getcwd (). "/" ;  
// Initialize the filecount variable $filecount = 0; $files2 = glob ( $directory . "*" ); if ( $files2 ) { $filecount = count ( $files2 ); } echo $filecount . "files" ;  
?>
Output:
20 files
openddir() function : function openddir() is used to open a directory handle. The path to the directory to open is sent as a parameter to the opendir() function, and it returns the directory handle resource on success or FALSE on error.Syntax:
 opendir (string $path, resource $context)
readdir() function: function readdir() - is a directory function that reads the entries from the directory descriptor, which are returned in the order they are stored in the file system.Syntax:
readdir ( resource $dir_handle)
Program 2:This program uses the openddir() and readdir() functions to count all files in a directory.  
// Install 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 ( ’.’ , ’ ..’ ) ) & amp; & amp;! is_dir ( $dir . $file )) $i ++; } }  
// Show result echo "$i files" ;  
?>
Output:
20 files
scandir() function: function scandir() is used to return an array of files and directories for the specified directory. The scandir() function lists the files and directories that are present in the specified path.Syntax:
scandir (string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING , resource $context)
Program 3:This program uses the scandir() and count() functions to count all files in a directory.  
// Set current working directory $directory = getcwd (). "/" ;  
// Returns an array of files $files1 = scandir ( $directory );  
// Count the number of files and store them in a variable $num_files = count ( $files1 ) - 2; echo $num_files . "files" ;  
?>
Output:
20 files
FilesystemIterator() function:FilesystemIterator::__ construct() function is used to create a new filesystem iterator along the path.Syntax:
FilesystemIterator::__ construct (string $path, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)
The iterator_count() function:The iterator_count() SPL function is used to count the elements of the iterator.Syntax:
int iterator_count ( $iterator)
Program 4:This program uses the FilesystemIterator() and iterator_count() functions to count all the files in a directory.

$fi = new FilesystemIterator (__ DIR__, FilesystemIterator::SKIP_DOTS); printf ( "% d files " , iterator_count ( $fi ) );  
?>
Output:
20 files
SO 1 data error