On Tue, 21 Aug 2001 21:56, Seb Frost wrote:
> OK so I'vebeen reading about reading and writing to/from files, but
> what I want to do is (I think) simpler than this.
>
> Basically I want to look at a directory and be able to get a list of
> all subdirectories and then a list of the files in each directory.  Can
> someone give me a hint?
>
> cheers....
>
> - seb


Goody - a recursive routine. I have just the thing-

// Dirs you don't want listed
$badlist = "secret phpMyAdmin stats test tools";
$bad_dirs = explode(" ", $badlist);
$docroot = "/usr/local/apache/htdocs/";
$basedir = $docroot;

function read_files($dirname) {
  global $filelist;
  $dirid = opendir($dirname);
  while($entry = readdir($dirid)):
    $this = $dirname . '/' . $entry;
    if ( (is_file($this)) && ($entry != ".") && ($entry != "..") && 
(!eregi("old$",$entry)) && (!eregi("bck$",$entry)) && (strstr($this, 
".htm") || strstr($this, ".html") || strstr($this, ".php"))):
      $filelist[] = $entry;
    endif;
  endwhile;
}

function exclude_dir($dirname) {
  global $bad_dirs;
  $outcome = false;
  while(list($key, $value) = each($bad_dirs)):
    if (strstr("$dirname", "$value")):
      $outcome = true;
    endif;
  endwhile;
  reset($bad_dirs);
  return $outcome;      
}

function recurse_dir($basedir) {
  global $dirlist;
  $dirid = opendir("$basedir");
  while ($entry = readdir($dirid) ) {
    $this = $basedir . $entry;
      if ( (is_dir($this)) && ($entry != ".") && ($entry != "..") && 
(!exclude_dir("$this/"))) {
        $dirlist[] = $this;
        recurse_dir("$this/");
      };
    };
};
?>

Call it with recurse_dir(dir-to_start) and get the results in $dirlist[]
Have fun. And yes, there's room for improvement, having looked at it for 
the first time in a year or so :-) The first obvious would be to return 
$dirlist....

-- 
David Robley      Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES      Flinders University, SOUTH AUSTRALIA  

   Oxymoron: Sugarless Candy.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to