Reputation: 5471
I'm trying to use scandir to display a select list of folders listed in a specific directory (which works fine) however, I need it to also add the child folders (if there are any) into my select list. If anyone could help me, that would be great!
This is the structure I want:
<option>folder 1</option>
<option> --child 1</option>
<option> folder 2</option>
<option> folder 3</option>
<option> --child 1</option>
<option> --child 2</option>
<option> --child 3</option>
And this is the code I have (which only shows the parent folders) which I got from this thread (Using scandir() to find folders in a directory (PHP)):
$dir = $_SERVER['DOCUMENT_ROOT']."\\folder\\";
$path = $dir;
$results = scandir($path);
$folders = array();
foreach ($results as $result) {
if ($result == '.' || $result == '..') continue;
if (is_dir($path . '/' . $result)) {
$folders[] = $result;
};
};
^^ but I need it to show the child directories also. I don't want the files, only the folders.
Upvotes: 2
Views: 7604
Reputation: 1222
You can use PHP "glob" function http://php.net/manual/en/function.glob.php , and build a recursive function (a function that call itself) to go infinite level depth. It's shorter then using "scandir"
function glob_dir_recursive($dirs, $depth=0) {
foreach ($dirs as $item) {
echo '<option>' . str_repeat('-',$depth*1) . basename($item) . '</option>'; //can use also "basename($item)" or "realpath($item)"
$subdir = glob($item . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR); //use DIRECTORY_SEPARATOR to be OS independent
if (!empty($subdir)) { //if subdir array is not empty make function recursive
glob_dir_recursive($subdir, $depth+1); //execute the function again with current subdir, increment depth
}
}
}
Usage:
$dirs = array('galleries'); //relative path examples: 'galleries' or '../galleries' or 'galleries/subfolder'.
//$dirs = array($_SERVER['DOCUMENT_ROOT'].'/galleries'); //absolute path example
//$dirs = array('galleries', $_SERVER['DOCUMENT_ROOT'].'/logs'); //multiple paths example
echo '<select>';
glob_dir_recursive($dirs); //to list directories and files
echo '</select>';
This will generate exactly the requested output type.
Upvotes: 2
Reputation: 1104
/* FUNCTION: showDir
* DESCRIPTION: Creates a list options from all files, folders, and recursivly
* found files and subfolders. Echos all the options as they are retrieved
* EXAMPLE: showDir(".") */
function showDir( $dir , $subdir = 0 ) {
if ( !is_dir( $dir ) ) { return false; }
$scan = scandir( $dir );
foreach( $scan as $key => $val ) {
if ( $val[0] == "." ) { continue; }
if ( is_dir( $dir . "/" . $val ) ) {
echo "<option>" . str_repeat( "--", $subdir ) . $val . "</option>\n";
if ( $val[0] !="." ) {
showDir( $dir . "/" . $val , $subdir + 1 );
}
}
}
return true;
}
Upvotes: 2
Reputation: 97835
//Requires PHP 5.3
$it = new RecursiveTreeIterator(
new RecursiveDirectoryIterator($dir));
foreach ($it as $k => $v) {
echo "<option>".htmlspecialchars($v)."</option>\n";
}
You can customize the prefix with RecursiveTreeIterator::setPrefixPart
.
Upvotes: 7