Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * DCS200 Week 4
- *
- * fileInfo version 0
- *
- * This scripts prints information about the files in a directory.
- *
- * Update the file_info function and the headers, width arrays to
- * print more file information.
- *
- * The script uses the printf function for formated output (which is
- * modelled on the C function). You can read about printf if you wish
- * to change the layout, but this is not necessary for the early steps
- */
- date_default_timezone_set('Europe/London');
- // The arrays header and width control the width of colums and the header
- // The arrays should be the same length. Extra columns can be added to the
- // arrays.
- $headers = array("File Name", "Size","Type","Modified") ; // column headers
- $width = array(20, 8, 8, 24) ; // max width of data in each column
- // The glob function returns an array of file names matching the pattern
- // in the current directory. Here the pattern is to match everything.
- $indent = 0;
- print "\n";
- print_header();
- $dirinfo = getFileTree(getcwd());
- array_walk($dirinfo["results"],'recursivePrint');
- print "\n";
- exit(0) ;
- function recursivePrint($item,$key){
- global $indent;
- if(is_array($item["results"])){
- if(!empty($item["results"])){
- if(!isset($item["dir"])){
- print_file_info($item["results"]);
- } else {
- $indent+=2;
- print "\n";
- printf("%-{$indent}.{$indent}s","");
- print "Directory /$key/\n";
- print_header();
- array_walk($item["results"],'recursivePrint');
- $indent-=2;
- }
- } else {
- print "\n/$key/\n";
- printf("%-{$indent}.{$indent}s","");
- print "- Empty Directory\n";
- }
- }
- // $indent-=2;
- }
- function getFileTree($currentDirectory) {
- $currentDirectory = rtrim($currentDirectory,"/")."/"; //make sure only one trailing slash on the end
- $exclude = array(".","..",".DS_Store"); //. and .. are parent dirs and I'm testing on my mac so .DS_STORE;
- $results = array();
- if(!is_dir($currentDirectory)){
- return false;
- }
- if(!$dh=opendir($currentDirectory)){
- return false;
- } else {
- while(($file=readdir($dh))!==false){
- if(!in_array($file,$exclude)){ //no excluded files
- if(is_dir($currentDirectory.$file."/")){ //we found a subdirectory
- if(strcmp($file,".")!=0 && strcmp($file,".."!=0)){ //no parent dirs!
- $results[$file]=getFileTree($currentDirectory.$file."/");
- }
- } else {
- $results[] = array("results"=>getFileInfo($currentDirectory.$file,$file),"file"=>true);
- }
- }
- }
- }
- return array("results"=>$results,"dir"=>true);
- }
- function getFileInfo($filename,$name){
- return array(
- $name,
- filesize($filename),
- filetype($filename),
- date("F d Y H:i",filemtime($filename))
- );
- }
- /*
- * Print the file information
- *
- * There shoud be no need to modify this part of the script, provided
- * that the width and info arrays are updated correctly
- */
- function print_file_info($info) {
- global $width, $indent;
- $fldNum = 0 ;
- while ($fldNum < count($info)) {
- printf("%-{$indent}.{$indent}s","");
- printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $info[$fldNum]) ;
- $fldNum++ ;
- }
- print "\n" ;
- }
- /*
- * Print a header
- *
- * There shoud be no need to modify this part of the script, provided
- * that the width and headers arrays are updated correctly
- */
- function print_header() {
- global $width, $headers,$indent ;
- $fldNum = 0 ;
- while ($fldNum < count($headers)) {
- printf("%-{$indent}.{$indent}s","");
- printf("%-{$width[$fldNum]}.{$width[$fldNum]}s", $headers[$fldNum] ) ;
- $fldNum++ ;
- }
- print "\n" ;
- // print a line
- $len = 0 ;
- foreach ($width as $w) { $len += $w ; }
- printf("%-{$indent}.{$indent}s","");
- printf("%'-{$len}s\n", "") ;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment