PHP嵌套文件树

I have searched on google and on this site for something similar to what I want but nothing fits exactly and all my efforts to adapt them have failed, I want to make a script that will map out its directory and all its subfolders and the folders be at the top of the subdirectory and files after them. So far I have come up with this:

<?php
$rawParent = scandir('.');
$parentDir = array_diff($rawParent, array('.', '..','cgi-bin','error_log'));

$arrayOfDirectories = array();
$arrayOfFiles = array();

foreach ($parentDir as $child){
    if(is_dir($child)){
        array_push($arrayOfDirectories,$child);
    }else{
        array_push($arrayOfFiles,$child);
    }
}
foreach ($arrayOfDirectories as $directory){
    echo $directory.'<br>';
}
echo "<br>";
foreach ($arrayOfFiles as $file){
    echo "<a href='".$file."'>".$file.'</a><br>';
}
?>

It's good so far but it only does the first level of the directory, can this code be adapted to go through all levels of folders and nest them? If so how? I need a few pointers, I will further use javascript to have toggles on the folders to see the contents, so I will need PHP to output something nested. Sorry if I am not making much sense, don't really know how to explain.

Use recursive function like this :

<?php
function list_directory($directory)
{ 
    $the_directory = opendir($directory) or die("Error $directory doesn't exist");
    while($file = @readdir($the_directory))
    { 

        if ($file == "." || $file == "..") continue;


        if(is_dir($directory.'/'.$file))
        { 
            print '<ul>'.$directory.'/'.$file;
            list_directory($directory.'/'.$file);
            print '</ul>';
         }
        else
        { 
            print "<li> $file  </li>";
         }

     }

    closedir($the_directory);
 }

$path_to_search = '/var/www';
list_directory($path_to_search);
 ?>

Version with storage in array :

<?php
function list_directory($directory, &$storage)
{ 
    $the_directory = opendir($directory) or die("Error $directory doesn't exist");
    while($file = @readdir($the_directory))
    { 

        if ($file == "." || $file == "..") continue;


        if(is_dir($directory.'/'.$file))
        { 
            list_directory($directory.'/'.$file, $storage);
         }
        else
        { 
            $storage[] = $file;
         }

     }

    closedir($the_directory);
 }
$storage = array();
$path_to_search = '/var/www';
list_directory($path_to_search, $storage);
echo '<pre>', print_r($storage,true) , '</pre>';
 ?>

This will do what you asked for, it only returns the sub directory names in a given path, and you can make hyperlinks and use them.

$yourStartingPath = "your string path";
   $iterator = new RecursiveIteratorIterator( 
        new RecursiveDirectoryIterator($yourStartingPath),  
    RecursiveIteratorIterator::SELF_FIRST); 

   foreach($iterator as $file) { 
     if($file->isDir()) { 
        $path = strtoupper($file->getRealpath()) ; 
        $path2 = PHP_EOL;
        $path3 = $path.$path2;
        $result = end(explode('/', $path3)); 
        echo "<br />". basename($result);
       } 
   }