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.
/* 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;
}