script to list directories won't work
Posted: Thu Dec 25, 2003 10:52 pm
Hey, yeah, I'm writing a script to list the files and subdirectories of a directory (it's like a tree command). The code is
The output was
Now, in reality, i know that the tree looks like this
So obviously, there's some kind of problem when the function comes out of the recursion that doesn't get the rest of the subdirectories. Help, anyone?
Code: Select all
<?php
function listDir($r00t, $tab)
{
if(is_dir($r00t))
{
$n = 0;
if(!($dh = opendir($r00t))) echo "Failed to open directory $r00t <br>";
else while(false !== ($fille = readdir($dh)))
{
echo $tab.$n.' '.$fille.'<br>';
$n++;
if(substr($fille, 0, 1) != '.') listDir($r00t.'/'.$fille, $tab.$tab);
}
closdir($dh);
}
}
listDir(getcwd().'/pi','    ');
?>Code: Select all
0 .
1 ..
2 table3.htm
3 172
0 .
1 ..
2 23
0 .
1 ..
2 testbinarysearch.phpCode: Select all
pi (dir)
- files
- 172 (dir)
- files
- 23 (dir)
- 24 (dir)
- 173 (dir)
- more stuff