Jump to content

Getting all the sub directories in a folder.


cooldude832

Recommended Posts

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'));

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 ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.