Try this
<?php
/*
base is where we start from it never changes
directory starts off with the base however it will
eventually hold subfolders and other folders of the base
*/
function getDir($base, $directory)
{
if (is_dir($directory))
{
if (substr($directory, -1) == "/")
$path = substr($directory, 0, -1); //get rid of /
$dh = opendir($directory);
while (($file = readdir($dh)) !== false)
{
if ($file != '.' && $file != '..')
if (is_dir($directory."/".$file))
{
echo str_repeat(" ", pad($base, $directory."/".$file) * 3). $file."<br />";
getDir($base, $directory."/".$file);
}
}
}
closedir($dh);
}
/*
This function finds out exactly how far deep we are
within the base and returns the level which we will then
use again getDir. If the directory is only a sublevel e.g.
first level of base we wont pad
*/
function pad($base, $curDir)
{
if (substr($base, -1) != '/')
$base .= '/';
$baseLength = strlen($base);
$dirAbovebase = substr($curDir, $baseLength);
$size = sizeof(explode("/", $dirAbovebase));
if ($size == 1)
return 0;
else
return $size - 1;
}
getDir('your_start_dir', 'your_start_dir');
?>
Hopefully the comments explain a little. However to get this to work you need to know where you started from, thus why we have $base which never changes and $directory which when we call the function should be identical to $base. Of course $directory will change. As we go deeper into the directory structure.
The pad function works out how far we are it isnt a fool proof function but it seemed to work quite well for myself anyway. The function basically returns a value of type Int which we then times with 3 to give the spaces. Now if we are in the first level i.e. the first sub directory of the base we return 0 and as we all know 0 * 3 = 0 thus no spacing be given. Eventually there will be however we need to -1 to it as it will give a result 1 bigger than it should.
I hope this is what your after or just on there.