5

I am trying to get a list with folders and sub folders i have the following that allows me to get the folders and sub folders but i needed it to be sorted out like the e.g below i have been trying but i dont know how i would get around.

Root/
Root/Images
Root/Images/UserImages

Root/Text/
Root/Text/User1/
Root/Text/User1/Folder2

but at the monent its display like this

Root/css/
tree/css/
js/
images/

PHP CODE:

    function ListFolder($path)
{

    $dir_handle = @opendir($path) or die("Unable to open $path");

    //Leave only the lastest folder name
    $dirname = end(explode("/", $path));

    //display the target folder.
    echo ("$dirname/");
    while (false !== ($file = readdir($dir_handle)))
    {
        if($file!="." && $file!="..")
        {
            if (is_dir($path."/".$file))
            {
                //Display a list of sub folders.
                ListFolder($path."/".$file);
                echo "<br>";
            }
        }
    }


    //closing the directory
    closedir($dir_handle);
}

    ListFolder("../");

Thank you

Rickstar
  • 6,057
  • 21
  • 55
  • 74
  • 1
    possible duplicate of at least [PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree](http://stackoverflow.com/questions/2418068/php-spl-recursivedirectoryiterator-recursiveiteratoriterator-retrieving-the-full) – Gordon Nov 17 '10 at 13:24

3 Answers3

4

Collect the directory names in an array instead of echoing them directly. Use sort on the array and a foreach-loop to print the list.

So instead of echo ("$dirname/"); you would use $dirnames[] = $dirname; (make $dirnames global and initialize it before your first call of "ListFolder"). Then after the recursive run of "ListFolder", you'd execute sort($dirnames); and then something like this for the output:

foreach ($dirnames as $dirname)
{
  echo $dirname . '<br />';
}
Select0r
  • 12,234
  • 11
  • 45
  • 68
1

with this code, you will get lists with subdirectories (but set your foldername)

<?php
$path = realpath('yourfolder/examplefolder');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
        echo "$filename\n";
}
?>
T.Todua
  • 53,146
  • 19
  • 236
  • 237
1

you can achive what you want with the DirectoryIterator or better the RecursiveDirectoryIterator from the php SPL.

here is a quick example on how to use this:

    $dir = '/path/to/directory';
    $result = array();

    if (is_dir($dir)) {
            $iterator = new RecursiveDirectoryIterator($dir);
            foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {                    
                if (!$file->isFile()) {
                    $result[] = 'path: ' . $file->getPath(). ',  filename: ' . $file->getFilename();
                }
            }

    }

This should do the trick. Good luck ;)

misterjinx
  • 2,606
  • 3
  • 27
  • 29
  • It would (probably) be preferable to skip `is_dir()` and catch the `UnexpectedValueException` thrown by `RecursiveDirectoryIterator` if `$dir` is not a directory; the latter is going to check for the directory anyway. You could also wrap the iterators in a `ParentIterator`, to list only directories, and forgo the `isFile()` condition. – salathe Nov 30 '10 at 09:59
  • yes, you are right, i could and should do that, but here i was just trying to show him a quick example. thanks! – misterjinx Nov 30 '10 at 10:33