home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / treeclimber.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  3.0 KB  |  94 lines

  1. <? 
  2. # Sample usage: 
  3. $t = new TreeClimber(  "/opt/mysql" ); 
  4. echo arrayValuesToString( $t->getFileList( $t->getPath() ),  "<BR>\n" ); 
  5.  
  6. #============================================================ 
  7. # Takes an array and turns it's VALUES into a delimited string. 
  8. # Pass the delimiter as arg 2 (defaults to nothing); 
  9. # If you pass false as parm 3, it will NOT add the delimiter to the LAST 
  10. # element, which is useful if you want to combine email addresses  
  11. # into a To: line, for example, and don't want the , (passed as $nl) to 
  12. # be added to the last element. 
  13. function arrayValuesToString( $ar, $nl= "", $dolast=true ) { 
  14.   $str =  ""; 
  15.   reset( $ar ); 
  16.   $size = sizeof( $ar ); 
  17.   $i = 1; 
  18.   while( list( $k, $v ) = each( $ar ) ) { 
  19.     if ( $dolast == false ) { 
  20.       if ( $i < $size ) { 
  21.     $str .= $ar[$k].$nl; 
  22.       } 
  23.       else { 
  24.     $str .= $ar[$k]; 
  25.       } 
  26.     } 
  27.     else { 
  28.       $str .= $ar[$k].$nl; 
  29.     } 
  30.     $i++; 
  31.   } 
  32.   return $str; 
  33. ?> 
  34.  
  35. <? 
  36. # A utility for recursively climbing down directories and getting a list of all of the files. 
  37.  
  38. class TreeClimber { 
  39.  
  40. var $path; 
  41. var $fileList = array(); 
  42.  
  43. function TreeClimber( $path =  "." ) { 
  44.   $this->path = $path; 
  45.  
  46. ######################################################################## 
  47. # accessors for path... 
  48. function getPath() { return $this->path; } 
  49. function setPath( $v ) { $this->path = $v; } 
  50.  
  51. #============================================================ 
  52. # Returns a list of all files in the given dir, excluding . and .. 
  53. # If no dir is passed, it uses the current setting for path (set 
  54. # in the constructor, or via setPath(). 
  55. # This function does NOT reset this object's path 
  56. # to the given path (it would screw up recursion). 
  57. # Returns null if it can't open the dir. 
  58. # If reset is passed as false, then it will not delete  
  59. # the current list of files when calling this function- 
  60. # this is intended to only be used for recursion into 
  61. # this function. 
  62. # Does not return DIRECTORIES in the file list unless $returnDirs 
  63. # is passed as true. 
  64. function getFileList( $dirname=null, $returnDirs=false, $reset=true ) { 
  65.   if ( $dirname == null ) { $dirname = $this->path; } 
  66. #  else { $this->setPath( $dirname ); } 
  67. #  dout( "Recursing into $dirname..." ); 
  68.   if ( $reset ) {  
  69.     $this->fileList = array(); 
  70.   } 
  71.   $dir = opendir( $dirname ); 
  72.   if ( ! $dir ) {  
  73.     print(  "<B><FONT COLOR=#FF0000>Warning: TreeClimber.getFileList( $dirname ): could not open $dirname!</FONT></B>" ); 
  74.     return null;  
  75.   } 
  76.   while( $file = readdir( $dir ) ) { 
  77.     if ( ereg(  "^\.$", $file ) || ereg(  "^\.\.$", $file ) ) continue; 
  78.     if ( is_dir( $dirname. "/".$file ) ) { 
  79.       $this->getFileList( $dirname. "/".$file, $returnDirs, false ); 
  80.       if ( $returnDirs ) { $this->fileList[] = $dirname. "/".$file;} 
  81.     } 
  82.     else { $this->fileList[] = $dirname. "/".$file; } 
  83.   } 
  84.   sort( $this->fileList ); 
  85.   return $this->fileList; 
  86.  
  87.  
  88. }  # end class TreeClimber 
  89. ?> 
  90.  
  91.