By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,521 Members | 1,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,521 IT Pros & Developers. It's quick & easy.

getting all sub-directories

P: n/a
Hi,

I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux. Given a
directory, how would I get all sub-directories? I found "scandir" but
that only applies for PHP 5 and I think it returns both files and
folders, whereas I only want the folders.

Thanks for any help, - Dave
Feb 11 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
This function will do it:

function getFilesByType($dir, $type = 'dir')
{
if (($dh = opendir($dir)) === false) {
trigger_error('opendir', E_USER_ERROR);
} else {
if ($dir[strlen($dir) -1] == DIRECTORY_SEPARATOR) {
$sep = '';
} else {
$sep = DIRECTORY_SEPARATOR;
}
$list = array();
while (($ent = readdir($dh)) !== false) {
if ($ent == '.' || $ent == '..') continue;
if (filetype("$dir$sep$ent") == $type) {
$list[] = "$dir$sep$ent";
}
}
closedir($dh);
return $list;
}
return false;
}

On Feb 10, 8:00 pm, "laredotorn...@zipmail.com"
<laredotorn...@zipmail.comwrote:
Hi,

I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux. Given a
directory, how would I get all sub-directories? I found "scandir" but
that only applies for PHP 5 and I think it returns both files and
folders, whereas I only want the folders.

Thanks for any help, - Dave
Feb 11 '08 #2

P: n/a
..oO(la***********@zipmail.com)
>I'm using PHP 4.4.4 with Apache 2 on Fedora Core 6 Linux.
PHP 4 is dead, PHP 6 on the rise. It's time for an update.
>Given a
directory, how would I get all sub-directories? I found "scandir" but
that only applies for PHP 5 and I think it returns both files and
folders, whereas I only want the folders.
I suggest to update to PHP 5 and use the iterators from the SPL. Then
with a simple 'foreach' loop you can easily loop through arbitrarily
deep nested tree structures and search for whatever you like, e.g.

$result = array();
foreach (new DirectoryIterator($yourPath) as $item) {
if ($item->isDir() && !$item->isDot()) {
$result[] = $item->getPathname();
}
}

Micha
Feb 11 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.