<?php # --------------------------------------------------------------- # class read_dir # # params: start directory and file-suffix # returns an array containing directories and files # depending on state passed to get_file_arr # 1 return array of pathes # 2 return path-array, last dir is array-key; # 3 return path-array, last dir is array-key, incl empty dirs # 0 return file-array; # # use: # $myvar = new read_dir($path_to_startdir, "jpg"); # $mycol = $myvar->get_file_arr(1); # # author: Joachim Wendenburg# created: 27.03.2004 # # modified: 23.06.2005 # item: $dir_arr added # # modified: 14.11.2008 # item: false !== ($file = readdir($dir)) thanks Tom! # avoid symlink trouble by comparing with realpath # passing arr as reference in read_dir_recursive # # -------------------------------------------------------------- class read_dir { var $file_suffix; var $file_arr = array(); var $path_arr = array(); var $coll_arr = array(); var $dir_arr = array(); var $stat; var $error = ""; function read_dir ($dir_start, $file_suffix) { # keep start directory and inage-suffix in mind # $this->dir_start = $dir_start; $this->file_suffix = $file_suffix; } # if stat = 0 return array of files # if stat = 1 return multi dimensional array of pathes # if stat = 2 return array of pathes using last dir as key # if stat = 3 same as 2 but contains also empty dirs # function get_file_arr ($stat) { $this->stat = $stat; # check if start-directory exists # call read_dir to read out directory # return array of content # if (file_exists($this->dir_start)) { $this->read_dir_recursive($this->dir_start, $this->file_arr); # what to return # switch ($this->stat) { case 1 : return $this->path_arr; case 2 : return $this->coll_arr; case 3 : return $this->coll_arr; // including empty directories default : return $this->file_arr; } } else { $this->error .= "Start directory does not exist"; return false; } } function read_dir_recursive ($path, &$arr) { # collect all pathes, even if directory is empty or does not contain matching files # $this->dir_arr[] = $path; # able to open directory? # if ($dir = @opendir($path)) { # flag if directory is emty or does not contain matching files # set false if dir or file is found # $is_last = true; # loop through all files # while (false !== ($file = readdir($dir))) { # ignore parent directory # if (preg_match("/^\./", $file)) continue; # if file is a directory start again # set directory name as array key # if (is_dir($path . "/" . $file)) { # do nothing if symlink detected # if (realpath($path . "/" . $file) !== $path . "/" . $file) continue; $is_last = false; $this->read_dir_recursive ($path . "/" . $file, $arr[$file]); } # if no directory check if suffix matches # add path to path array # add file to file array # MODIFY REGEX IF YOU EXPECT DIFFERENT FILENAMES # elseif (preg_match("/^[\w]{3,40}\." . $this->file_suffix . "$/", $file)) { $is_last = false; # create collection key from parent directory name # $coll_key = substr($path, strrpos($path, "/") +1); # add akt path and file to path array # add akt path and file to collection array # add file to file array # $this->path_arr[] = $path . "/" . $file; $this->coll_arr[$coll_key][] = $path . "/" . $file; $arr[] = $file; } } closedir($dir); # if directory is emty or does not contain matching files # add empty directory # if ($is_last && $this->stat === 3) { $coll_key = substr($path, strrpos($path, "/") +1); $this->coll_arr[$coll_key] = false; } } else { # unable to read a subdirectory # $this->error .= "Error: unable to open subdirectory " . $dir; } } } ?>