Tags
,
Asked 30+ days ago
28 Aug 2017
Views 202

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 Jan 22 '18 20:17

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;
}
	}
}
 
Edit Answer
Share
Post Answer