php Scan All Files in Specified Directory Example Using scandir of Function
- 2021-12-12 03:57:04
- OfStack
This article example shows that php uses the scandir () function to scan all files in a specified directory. Share it for your reference, as follows:
// Traverse the contents of subfolders and folders And calculate the number of files
//1 A demo Replace quotation marks below
function scan($dir){
static $i=0;
static $d=0;
$dirArr = scandir('.');
foreach($dirArr as $v){
if($v!='.' && $v!='..'){
$dirname = $dir."\\".$v; // Directory address of subfolder
if(is_dir($dirname)){
$count['dir'] = ++$d;
scan($dirname);
}
$count['file'] = ++$i;
}
}
return $count;
}
$count = scan('.');
echo "1 Common file {$count['file']} A , Folder {$count['dir']} A ";
Category of document
// For the current directory
$dir = scandir('.');
foreach($dir as $v){
$base = strrchr($v,'.');
if($v!='.'){
if($v == '..'){
$type =' Superior directory ';
}
// Type not 11 Example
if(is_dir($v)){
$type =' Directory ';
}elseif($base== '.jpg' || $base=='.gif'){
$type =' Picture file ';
}elseif($base== '.rar' || $base=='.zip'){
$type =' Zip file ';
}else{
$type=" Documents ";
}
}
}
More readers interested in PHP can check the topics of this site: "Summary of PHP Directory Operation Skills", "Summary of php File Operation", "Summary of PHP Common Traversal Algorithms and Skills", "Tutorial on PHP Data Structure and Algorithms", "Summary of php Programming Algorithms", "Complete Collection of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.