Asked 8 years ago
20 Dec 2016
Views 8229
noob

noob posted

how to get list of all directory of given path in PHP

scandir give me all list of files and directory for given path

i need only directory of given path not files.

i dont need . or .. or files , only directories.

so suggest me any direct function in PHP which give me list of only directories not files in given path.

how to list all directory of given path in PHP ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

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;
}
	}
}
 
shyam

shyam
answered Nov 30 '-1 00:00

how to get directory list without scandir function in php


$iterator = new DirectoryIterator($path);
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot ()) {
        echo $fileinfo->getFilename() . "\n";
    }
}


it will give you all directory for provided physical path at php
i used DirectoryIterator which had isDir function by which one can check is the file is directory or not ?
and ! isDot function will omit "." and ".."

is pretty simple and no extra code . its genius
ravi

ravi
answered Nov 30 '-1 00:00

use scandir function to get list of directory and files in php
following is simplified code of it .


function list_folders( $folder = '', $levels = 100 ) {
	if ( empty($folder) )
		return false;

	if ( ! $levels )
		return false;

	$files = array();
	if ( $dir = @opendir( $folder ) ) {
		while (($file = readdir( $dir ) ) !== false ) {
			if ( in_array($file, array('.', '..') ) )
				continue;
			if ( is_dir( $folder . '/' . $file ) ) {
				$files2 = list_folders( $folder . '/' . $file, $levels - 1);
				if ( $files2 )
					$files = array_merge($files, $files2 );
				else
					$files[] = $folder . '/' . $file . '/';
			}  
		}
	}
	@closedir( $dir );
	return $files;
}

apply it

$files=list_folders(dirname(__FILE__),2);
print_r($files);

here first argument is where to find folders and second argument is how much level deep you want to go .
rajiv

rajiv
answered Feb 28 '23 00:00

To get a list of all directories of a given path in PHP, you can use the scandir() function to retrieve a list of all files and directories in the path, and then filter the results to only include directories. Here's an example:



$path = '/path/to/directory';

// Get a list of all files and directories in the path
$files = scandir($path);

// Filter the list to only include directories
$directories = array_filter($files, function($file) use ($path) {
    return is_dir($path . '/' . $file);
});


// Print the list of directories
print_r($directories);

In this example, we first set the $path variable to the path of the directory we want to get a list of directories for. We then use the scandir() function to retrieve a list of all files and directories in the path. Next, we use the array_filter() function to filter the list to only include directories. We do this by using the is_dir() function to check if each item in the list is a directory or not . Finally, we print the list of directories using print_r().

Note that the scandir() function also includes the special directories . and .. , which represent the current directory and the parent directory, respectively. You may want to filter these out if you only want to list directories that are contained within the given path.
Post Answer