PDA

View Full Version : recursive function



trefrog
15 May 2006, 10:34 AM
I'm trying to make a script that goes thru all the subfolders within a directory. Later I'll add a part to insert the folder names into a database. The problem is my function won't go any deeper than 2 levels and I haven't been able to figure it out. Can anyone see the problem with this?


function subfolders($folderIn = '', $levelIn = 0) {
$folderName = $folderIn;
$folderIn .= ($folderIn!='') ? '/' : '';
$levelOut = $levelIn+1;
$tmp = glob($folderIn.'*', GLOB_ONLYDIR);
foreach($tmp as $key=>$value) {
$dir[] = end(explode('/', $value));
$tab = ' ';
$str = '';
for($i=0; $i<$levelIn; $i++) { $str .= $tab; }
echo $str.$dir[$key]."\n";
subfolders($dir[$key], $levelOut);
}
}

echo '<pre>'."\n";
subfolders();
echo '</pre>'."\n";

trefrog
15 May 2006, 11:07 AM
nevermind... I wasn't giving glob() the full path.

subfolders($folderIn.$dir[$key], $levelOut);