Tags
PHP , scandir
Asked 7 years ago
28 Aug 2017
Views 1562
jaydeep

jaydeep posted

php : how scandir work

how scandir work in php ?


scandir() 


i researched that
scandir will List files and directories inside the specified path

but dont know how to use scandir in php ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

how scandir work ?

scandir wil collect all files and directories inside the given directory , it means it will scan directory and collect the file name and directory name and put it in array and return an array of directories and files .

Examples

Exercise -1 : get only directories by scandir in php
suppose if you want only directories
1. remove . and ..
2. collect all directories by checking is it directory or not by is_dir function or
is not file (!is_file)



CODE :
following is the code to remove files and remove . and ..

 
$list=scandir($path);
foreach($list as $value){
	  if($value!='..' &&  $value!="." && !is_file($path.'/'.$value) ){
	$directories[]=$value;
	}
}


$list will contain like this after scan





or you can check is it directory or not . if yes push it in array

$list=scandir($path);
foreach($list as $value){
	  if($value!='..' &&  $value!="."  ){
if( is_dir($path.'/'.$value)){
	$directories[]=$value;
}
	}
}
 
Post Answer