home *** CD-ROM | disk | FTP | other *** search
- <?
- # Sample usage:
- $t = new TreeClimber( "/opt/mysql" );
- echo arrayValuesToString( $t->getFileList( $t->getPath() ), "<BR>\n" );
-
- #============================================================
- # Takes an array and turns it's VALUES into a delimited string.
- # Pass the delimiter as arg 2 (defaults to nothing);
- # If you pass false as parm 3, it will NOT add the delimiter to the LAST
- # element, which is useful if you want to combine email addresses
- # into a To: line, for example, and don't want the , (passed as $nl) to
- # be added to the last element.
- function arrayValuesToString( $ar, $nl= "", $dolast=true ) {
- $str = "";
- reset( $ar );
- $size = sizeof( $ar );
- $i = 1;
- while( list( $k, $v ) = each( $ar ) ) {
- if ( $dolast == false ) {
- if ( $i < $size ) {
- $str .= $ar[$k].$nl;
- }
- else {
- $str .= $ar[$k];
- }
- }
- else {
- $str .= $ar[$k].$nl;
- }
- $i++;
- }
- return $str;
- }
- ?>
-
- <?
- # A utility for recursively climbing down directories and getting a list of all of the files.
-
- class TreeClimber {
-
- var $path;
- var $fileList = array();
-
- function TreeClimber( $path = "." ) {
- $this->path = $path;
- }
-
- ########################################################################
- # accessors for path...
- function getPath() { return $this->path; }
- function setPath( $v ) { $this->path = $v; }
-
- #============================================================
- # Returns a list of all files in the given dir, excluding . and ..
- # If no dir is passed, it uses the current setting for path (set
- # in the constructor, or via setPath().
- # This function does NOT reset this object's path
- # to the given path (it would screw up recursion).
- # Returns null if it can't open the dir.
- # If reset is passed as false, then it will not delete
- # the current list of files when calling this function-
- # this is intended to only be used for recursion into
- # this function.
- # Does not return DIRECTORIES in the file list unless $returnDirs
- # is passed as true.
- function getFileList( $dirname=null, $returnDirs=false, $reset=true ) {
- if ( $dirname == null ) { $dirname = $this->path; }
- # else { $this->setPath( $dirname ); }
- # dout( "Recursing into $dirname..." );
- if ( $reset ) {
- $this->fileList = array();
- }
- $dir = opendir( $dirname );
- if ( ! $dir ) {
- print( "<B><FONT COLOR=#FF0000>Warning: TreeClimber.getFileList( $dirname ): could not open $dirname!</FONT></B>" );
- return null;
- }
- while( $file = readdir( $dir ) ) {
- if ( ereg( "^\.$", $file ) || ereg( "^\.\.$", $file ) ) continue;
- if ( is_dir( $dirname. "/".$file ) ) {
- $this->getFileList( $dirname. "/".$file, $returnDirs, false );
- if ( $returnDirs ) { $this->fileList[] = $dirname. "/".$file;}
- }
- else { $this->fileList[] = $dirname. "/".$file; }
- }
- sort( $this->fileList );
- return $this->fileList;
- }
-
-
- } # end class TreeClimber
- ?>
-
-