2

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.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
SoulieBaby
  • 5,405
  • 25
  • 95
  • 145

3 Answers3

7
//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.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
2
/* 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;
}
abelito
  • 1,094
  • 1
  • 7
  • 18
  • thanks for this, but it's showing files aswell - I only want the folders themselves :) – SoulieBaby Jun 23 '10 at 02:27
  • Ahh, I fixed it for you :) If you need it to display the . and .., add the following lines after $scan = scandir: if ( $subdir == 0 ) { echo ""; } – abelito Jun 23 '10 at 03:04
  • wait figured it out, had to add in my $dir into is_dir($val) etc :D Thank you for your help! :) – SoulieBaby Jun 23 '10 at 03:21
  • lol, good. i just updated it with that exact same change. no problem, glad to help you get started! :) – abelito Jun 23 '10 at 03:33
2

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.

crisc2000
  • 1,082
  • 13
  • 19