hugosslade

ENOS:4:3

Oct 26th, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.     glob/file_info is now a big recursive function called getFileTree();
  5.     print_info/print_header are used within the new daddy of a print function - recursePrint();
  6. */
  7.  
  8. date_default_timezone_set('Europe/London');
  9. $headers = array("File Name", "Size","Type","Modified") ; // column headers
  10. $width = array(20, 8, 8, 24) ;                 // max width of data in each column
  11.  
  12.  
  13. $indent = 0;
  14.  
  15. print "\n";
  16. print_header();
  17.  
  18. $dirinfo = getFileTree(getcwd());
  19. array_walk($dirinfo["results"],'recursivePrint');
  20.  
  21. print "\n";
  22. exit(0) ;
  23.  
  24. function recursivePrint($item,$key){
  25.     global $indent;
  26.     if(is_array($item["results"])){
  27.         if(!empty($item["results"])){
  28.             if(!isset($item["dir"])){
  29.                 print_file_info($item["results"]);
  30.             } else {
  31.                 $indent+=2;
  32.                
  33.                 print "\n";
  34.                 printf("%-{$indent}.{$indent}s","");
  35.                 print "Directory /$key/\n";
  36.                
  37.                 print_header();
  38.                
  39.                 array_walk($item["results"],'recursivePrint');
  40.                
  41.             $indent-=2;
  42.                
  43.                
  44.             }
  45.         } else {
  46.             print "\n/$key/\n";
  47.                 printf("%-{$indent}.{$indent}s","");
  48.             print "- Empty Directory\n";
  49.         }
  50.     }
  51. }
  52.  
  53. function getFileTree($currentDirectory) {
  54.    
  55.     $currentDirectory = rtrim($currentDirectory,"/")."/"; //make sure only one trailing slash on the end
  56.    
  57.     $exclude = array(".","..",".DS_Store"); //. and .. are parent dirs and I'm testing on my mac so .DS_STORE;
  58.     $results = array();
  59.    
  60.     if(!is_dir($currentDirectory)){
  61.         return false;
  62.     }
  63.     if(!$dh=opendir($currentDirectory)){
  64.         return false;
  65.     } else {
  66.         while(($file=readdir($dh))!==false){
  67.             if(!in_array($file,$exclude)){ //no excluded files
  68.                 if(is_dir($currentDirectory.$file."/")){ //we found a subdirectory
  69.                     if(strcmp($file,".")!=0 && strcmp($file,".."!=0)){ //no parent dirs!
  70.                         $results[$file]=getFileTree($currentDirectory.$file."/");
  71.                     }
  72.                 } else {
  73.                     $results[] = array("results"=>getFileInfo($currentDirectory.$file,$file),"file"=>true);
  74.                 }
  75.             }
  76.         }
  77.     }
  78.     return array("results"=>$results,"dir"=>true);
  79.    
  80. }
  81.  
  82. function getFileInfo($filename,$name){
  83.     return array(
  84.         $name,
  85.         filesize($filename),
  86.         filetype($filename),
  87.         date("F d Y H:i",filemtime($filename))
  88.     );
  89. }
  90.  
  91.  
  92. function print_file_info($info) {
  93.     global $width, $indent;
  94.     $fldNum = 0 ;
  95.     while ($fldNum < count($info)) {
  96.         printf("%-{$indent}.{$indent}s","");
  97.         printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $info[$fldNum]) ;
  98.         $fldNum++ ;
  99.     }
  100.     print "\n" ;
  101. }
  102.  
  103. function print_header() {
  104.     global $width, $headers,$indent ;
  105.     $fldNum = 0 ;
  106.     while ($fldNum < count($headers)) {
  107.         printf("%-{$indent}.{$indent}s","");
  108.         printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $headers[$fldNum] ) ;
  109.         $fldNum++ ;
  110.     }
  111.     print "\n" ;
  112.     // print a line
  113.     $len = 0 ;
  114.     foreach ($width as $w) { $len += $w ; }
  115.         printf("%-{$indent}.{$indent}s","");
  116.     printf("%'-{$len}s\n", "") ;
  117. }
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment