hugosslade

ENOS:4:3 FULL

Oct 27th, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.80 KB | None | 0 0
  1. <?php
  2. /*
  3.  * DCS200 Week 4
  4.  *
  5.  * fileInfo version 0
  6.  *
  7.  * This scripts prints information about the files in a directory.
  8.  *
  9.  * Update the file_info function and the headers, width arrays to
  10.  * print more file information.
  11.  *
  12.  * The script uses the printf function for formated output (which is
  13.  * modelled on the C function). You can read about printf if you wish
  14.  * to change the layout, but this is not necessary for the early steps
  15.  */
  16.  
  17. date_default_timezone_set('Europe/London');
  18.  
  19.  
  20. // The arrays header and width control the width of colums and the header
  21. // The arrays should be the same length. Extra columns can be added to the
  22. // arrays.
  23. $headers = array("File Name", "Size","Type","Modified") ; // column headers
  24. $width = array(20, 8, 8, 24) ;                 // max width of data in each column
  25.  
  26.  
  27. // The glob function returns an array of file names matching the pattern
  28. // in the current directory. Here the pattern is to match everything.
  29.  
  30. $indent = 0;
  31.  
  32.  
  33. print "\n";
  34. print_header();
  35.  
  36. $dirinfo = getFileTree(getcwd());
  37. array_walk($dirinfo["results"],'recursivePrint');
  38.  
  39. print "\n";
  40. exit(0) ;
  41.  
  42. function recursivePrint($item,$key){
  43.     global $indent;
  44.     if(is_array($item["results"])){
  45.         if(!empty($item["results"])){
  46.             if(!isset($item["dir"])){
  47.                 print_file_info($item["results"]);
  48.             } else {
  49.                 $indent+=2;
  50.                
  51.                 print "\n";
  52.                 printf("%-{$indent}.{$indent}s","");
  53.                 print "Directory /$key/\n";
  54.                
  55.                 print_header();
  56.                
  57.                 array_walk($item["results"],'recursivePrint');
  58.                
  59.             $indent-=2;
  60.                
  61.                
  62.             }
  63.         } else {
  64.             print "\n/$key/\n";
  65.                 printf("%-{$indent}.{$indent}s","");
  66.             print "- Empty Directory\n";
  67.         }
  68.     }
  69.    
  70. //                  $indent-=2;
  71. }
  72.  
  73. function getFileTree($currentDirectory) {
  74.    
  75.     $currentDirectory = rtrim($currentDirectory,"/")."/"; //make sure only one trailing slash on the end
  76.    
  77.     $exclude = array(".","..",".DS_Store"); //. and .. are parent dirs and I'm testing on my mac so .DS_STORE;
  78.     $results = array();
  79.    
  80.     if(!is_dir($currentDirectory)){
  81.         return false;
  82.     }
  83.     if(!$dh=opendir($currentDirectory)){
  84.         return false;
  85.     } else {
  86.         while(($file=readdir($dh))!==false){
  87.             if(!in_array($file,$exclude)){ //no excluded files
  88.                 if(is_dir($currentDirectory.$file."/")){ //we found a subdirectory
  89.                     if(strcmp($file,".")!=0 && strcmp($file,".."!=0)){ //no parent dirs!
  90.                         $results[$file]=getFileTree($currentDirectory.$file."/");
  91.                     }
  92.                 } else {
  93.                     $results[] = array("results"=>getFileInfo($currentDirectory.$file,$file),"file"=>true);
  94.                 }
  95.             }
  96.         }
  97.     }
  98.     return array("results"=>$results,"dir"=>true);
  99.    
  100. }
  101.  
  102. function getFileInfo($filename,$name){
  103.     return array(
  104.         $name,
  105.         filesize($filename),
  106.         filetype($filename),
  107.         date("F d Y H:i",filemtime($filename))
  108.     );
  109. }
  110.  
  111. /*
  112.  * Print the file information
  113.  *
  114.  * There shoud be no need to modify this part of the script, provided
  115.  * that the width and info arrays are updated correctly
  116.  */
  117. function print_file_info($info) {
  118.     global $width, $indent;
  119.     $fldNum = 0 ;
  120.     while ($fldNum < count($info)) {
  121.         printf("%-{$indent}.{$indent}s","");
  122.         printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $info[$fldNum]) ;
  123.         $fldNum++ ;
  124.     }
  125.     print "\n" ;
  126. }
  127.  
  128. /*
  129.  * Print a header
  130.  *
  131.  * There shoud be no need to modify this part of the script, provided
  132.  * that the width and headers arrays are updated correctly
  133.  */
  134. function print_header() {
  135.     global $width, $headers,$indent ;
  136.     $fldNum = 0 ;
  137.     while ($fldNum < count($headers)) {
  138.         printf("%-{$indent}.{$indent}s","");
  139.         printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $headers[$fldNum] ) ;
  140.         $fldNum++ ;
  141.     }
  142.     print "\n" ;
  143.     // print a line
  144.     $len = 0 ;
  145.     foreach ($width as $w) { $len += $w ; }
  146.         printf("%-{$indent}.{$indent}s","");
  147.     printf("%'-{$len}s\n", "") ;
  148. }
  149. ?>
  150.  
Advertisement
Add Comment
Please, Sign In to add comment