Solved

How to create a list of all sub-Directories?

Posted on 2006-06-19
8
837 Views
Last Modified: 2010-05-18
I need to create a list of all sub-Directories in http://www.mydomain.com/images/
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.

0
Comment
Question by:MnInShdw
  • 4
  • 2
  • 2
8 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 250 total points
Comment Utility
<?php
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');

-r-
0
 
LVL 1

Author Comment

by:MnInShdw
Comment Utility
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/mydomain.com/tests/test.php on line 8
Array ( )
-----------------------------------------------------------------------

any kind of furter assist is much appreciated
0
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
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.

-r-
0
 
LVL 1

Author Comment

by:MnInShdw
Comment Utility
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.

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:ixti
Comment Utility
If you need more than just listing of subfolders, then, I suggest you to look at this class: http://www.phpclasses.org/browse/package/1294.html
0
 
LVL 1

Author Comment

by:MnInShdw
Comment Utility
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.

0
 
LVL 6

Accepted Solution

by:
ixti earned 250 total points
Comment Utility
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: http://php.pastebin.com/720237
0
 
LVL 1

Author Comment

by:MnInShdw
Comment Utility
Million thanks for your help
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now