Hi All

I have the following code which populates a list box with the names of folders within a folder that I specify as the root. However, my code is only displaying one level of folders within my root folder and I need it to display all sub folders within those folders (i hope that makes sense).

Here is my current code:


$path = "../mydocs/";
$dir_handle  =  @opendir($path)  or  die("Unable to open $path");  
while  (false !== ($file = readdir($dir_handle)))  {  
	if ($file == ".." || is_file($file))
	continue;
	if ($file == ".") {
		$file = "/";
	}

	echo "   <option value=\"$file\">$file</option>\n";
}
closedir($dir_handle);


Would someone be able to help me greatly by inserting code which will display the sub folders also?

I guess what I am looking for is my list box to look something like this when expanded:

/
/Level1
/Level1/sublevel1
/Level2
/Level2/sublevel2
/Level2/sublevel2/anotherfolder

Many Thanks for any help offered.

    I prefer to use

    [man]scandir/man

    it is easy to use and to understand
    then I can loop the array with filenames, foreach
    and do what I want

    <?php
    
    $dir    = './';
    $files = scandir($dir);
    echo '<pre>'.print_r($files, true).'</pre>';
    
    ?>

      Thanks halojoy but I am still needing help with my problem.

      scandir is a better alternative than my code which is good, however, i still need to output the sub folders via a list box. scandir does not search through sub folders, it only searches the folder specified.

      Can you or any other phpbuilder out there help please?

      Many Thanks

        Write a Reply...