A seemingly simple problem with(out) a seemingly impossible solution...
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";
echo $str.'<font color="red"> subfolders('.$dir[$key].', '.$levelOut.')</font>'."\n";
subfolders($dir[$key], $levelOut);
}
}
echo '<pre>'."\n";
subfolders();
echo '</pre>'."\n";
The echoes are for debugging. All I'm trying to make is a function that will get all folder names beneath the folder in which the script is running. This is for adding the information into a database table with hierarchial data (hence the $levelIn/Out). There are tons of folders and subdirectories I need to INSERT, way too many to do by hand. Currently, it only goes down 1 level, outputting (without debug echo), just one example:
Transportation
Airports & Airplanes
Automobiles
Bicycles
Boats
Bridges
Bus Lines
Camels
Canals
Ferries
Horses
Livery Stables
Motorcycles
Pack Trains
Railways
Rivers
Roads
Saddles
Stagelines
Tractors
Trails
Wagons
further subfolders are missing and I can't figure out why.