how to get list of all directory ?
scandir wil List files and directories inside the specified path , 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 for given path .
LETS MAKE ALGO
now if you want only files to get from scandir . you need to filter to array what you got returned from scandir
1. remove . and ..
2. collect all files by checkint is it file or not ?
NOW LETS 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
More detailed explanation
1. need to remove .. and . ,
$list=scandir($path);
foreach($list as $value){
if($value!='..' && $value!="."){
// now $value is only file and directory
}
}
2. remove the file also by checking is not file than put it in array if(!is_file($filename)){}//means is not file than go to in or out
.
so final code will be
$list=scandir($path);
foreach($list as $value){
if($value!='..' && $value!="." ){
if( !is_file($path.'/'.$value)){
$directories[]=$value;
}
}
}
2. collect directories
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;
}
}
}