cooldude832 Posted October 12, 2007 Share Posted October 12, 2007 I want to return all the subfolders in a given folder any idea? I thought glob can only do files. Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/ Share on other sites More sharing options...
cooldude832 Posted October 12, 2007 Author Share Posted October 12, 2007 should mention i can use scandir as i'm on 4 Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/#findComment-367549 Share on other sites More sharing options...
cooldude832 Posted October 12, 2007 Author Share Posted October 12, 2007 I did $dir = ROOT."discus"; $dh = opendir($dir) or die($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } but I can't get the thing to open it Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/#findComment-367553 Share on other sites More sharing options...
corbin Posted October 12, 2007 Share Posted October 12, 2007 Ahhh... Here's something I wrote like 6 months ago.... Looking at it now, it looks kind of... bleh, but hopefully it'll give you the right idea ;p. It's a recursive function, but it could be done without a recursive function. function ListFiles($folder) { $out = array(); $han = opendir($folder); $f = (!empty($folder)) ? $folder . '/' : ''; while($it = readdir($han)) { if($it == '.' || $it == '..') continue; if(!is_dir($f.$it)) { $out[] = (strlen($folder) > 0) ? $folder . '/' . $it : $it; continue; } $path = (strlen($folder) > 0) ? $folder . '/' . $it : $it; $out = array_merge($out, ListFiles($path)); } return $out; } print_r(ListFiles('some/folder')); Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/#findComment-367565 Share on other sites More sharing options...
cooldude832 Posted October 12, 2007 Author Share Posted October 12, 2007 I solved it using <?php $dir = "discus/messages"; $dh = opendir($dir) or die($dir); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } sort($files); for($i=2; $i<=62; $i++){ $folders[] = $files[$i]; } ?> the 2 to 62 range is the folders I am reading, only because I cheated and saw what the folders where Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/#findComment-367581 Share on other sites More sharing options...
corbin Posted October 12, 2007 Share Posted October 12, 2007 You could just do count($files) to get the 62, or you could do a foreach loop if you didn't want it statically set ;p. Link to comment https://forums.phpfreaks.com/topic/72874-getting-all-the-sub-directories-in-a-folder/#findComment-367583 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.