Asked 30+ days ago
28 Aug 2017
Views 199


php : how scandir work

how scandir work in php ?


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 21 '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 .


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)

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

foreach($list as $value){
	  if($value!='..' &&  $value!="." && !is_file($path.'/'.$value) ){

$list will contain like this after scan

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

foreach($list as $value){
	  if($value!='..' &&  $value!="."  ){
if( is_dir($path.'/'.$value)){
Edit Answer
Post Answer