How to create a list of all sub-Directories?

I need to create a list of all sub-Directories in
but it seems my knowledge of php is not enough for this task.

if the images directory contains two subdirectories, I need to have a variable
containing the names of the sub-directories.

Any kind of help is much appreciated.

Who is Participating?
ixtiConnect With a Mentor Commented:
I'm registered there. I don't know why, but I can't paste that class here (when I'm trying - error happens ('Queston ID not found...')
So I'll paste it on pastebin:
RoonaanConnect With a Mentor Commented:
function subDirs($mainDir) {
  // See if the maindir exists
  if(!is_dir($mainDir)) return false;
  // Create a handler to the directory
  $d = opendir($mainDir);
  // If handle fails, return false
  if(!$d) return false;
  // Storage variable
  $subdirs = array();
  // Read file and directorynames from $mainDir
  while($f = readdir($d)) {
    if($f == '..' || $f = '.') continue; //skip parent and current dir indicators
    if(is_dir($mainDir.'/'.$f)) $subdirs[] = $f;
  // Return our subdirs storage variable
  return $subdirs;

// Read subdirs in folder images
$subdirs = subDirs('images');

MnInShdwAuthor Commented:
Hi Roonaan and thanks for trying to help

I tried your code but unfortunately I'm not able to make it work.
There's 2 problems.

1- I added print_r($subdirs) as the last line of your code, but it displays a blank array     array()

2- the second problem is when I try to run this code on a directory with 711 permission, I receive
    the following error
Warning: opendir(images/): failed to open dir: Permission denied
in /hsphere/local/home/mydomain/ on line 8
Array ( )

any kind of furter assist is much appreciated
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

711 might be too tight. Note that most of the time php is run from the apache user, which most of the time is not in the group with the actual file/directory owner.

MnInShdwAuthor Commented:
I changed the permission to 744 and the script doesn't cause errors
but still the array is empty.
this is the structure of directories

ram ----> images   ----> thumbs1
                            ----> thumbs2
                            ----> thumbs3

your script is saved as test.php in ram directory.

If you need more than just listing of subfolders, then, I suggest you to look at this class:
MnInShdwAuthor Commented:
hello ixti

thank you for helping. the site you mentioned needs subscribing
and they don't accept free mails such as msn-hotmail-gmail....

I had to use a pop3 account and now they say I have to wait for 24
hours for being checked by admins. later on if I came out ok I'll be
allowed to download the class zip.

MnInShdwAuthor Commented:
Million thanks for your help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.