Method of counting the number of files in PHP directory
- 2021-11-29 06:33:32
- OfStack
The code example is as follows:
<?php
$folderPath = "upload/";
$countFile = 0;
$totalFiles = glob($folderPath . "*");
if ($totalFiles){
$countFile = count($totalFiles);
}
print_r($countFile);
Introduction of related functions:
The glob () function looks for a file path that matches the pattern
glob ( string $pattern [, int $flags = 0 ] ) : array
The glob () function looks for all file paths that match pattern according to the rules used by the libc glob () function, similar to Rule 1 used by shells. Abbreviation extension or parameter substitution is not performed. Returns an array containing matching files/directories. Returns FALSE if an error occurs.
Valid flags for parameter flags are:
GLOB_MARK - Add to each returned item 1 Slash
GLOB_NOSORT - Returns in the original order in which files appear in the directory (unsorted)
GLOB_NOCHECK - Returns the pattern for search if no file matches
GLOB_NOESCAPE - Backslashes do not escape metacharacters GLOB_BRACE - Expand {a,b,c} To match 'a' , 'b' Or 'c'
GLOB_ONLYDIR - Returns only directory entries that match the pattern
GLOB_ERR - Stop and read error messages (such as unreadable directories), and ignore all errors by default
The count () function counts the number of cells in an array, or the number of attributes in an object
count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] ) : int
Count the number of all elements in the array, or what is in the object. For objects, if SPL is installed, you can hook count () (hook) by implementing the Countable interface. The interface has only one method, Countable:: count (), which returns a value for the count () function.