The script fileInfo0.php below lists the names of file in a directory. Can you help to enhance and modify this to create fileInfo1.php which shows
o the size of the file,
o the type of the file and
o the date last modified.
The report should look similar to:
File Name Size Type Modified
--------------------------------------...
fileInfo0.php 2417 file Oct 20 2011 09:34
fileInfo0_old.php 1854 file Oct 20 2011 09:50
fileInfo1.php 2478 file Oct 20 2011 10:34
nbproject 170 dir Oct 20 2011 08:51

Create some files to demonstrate your program working, including directories. Note: dates are a little complex: you need to use the 'date' function to format the time stamp. There are lots of examples in the PHP manual. You may need to set the timezone which is used to display the dates. If so, execute the following statement once in the script: date_default_timezone_set('Europe/London... ;

Can you then describe the changes made to fileInfo0.php to get fileInfo1.php.

<?php
/*
*
*
* fileInfo0.php
*
* 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
*/


// 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", "Characters") ; // column headers
$width = array(20, 11) ; // max width of data in each column

// The info array contains the information files about the file
// This array is updated in the file_info function
$info ; // array of file information fields

// The glob function returns an array of file names matching the pattern
// in the current directory. Here the pattern is to match everything.
print_header() ;
foreach (glob("*") as $filename) {
file_info($filename) ;
print_file_info() ;
}
exit(0) ;

/*
* Get information about one file
*
* This function update the $info array
*
*/
function file_info($name) {
global $info ;
$info[0] = $name ; // the file name
$info[1] = strlen($name) ; // the number of characters in the name
// which is pointless - replace!
}

/*
* 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() {
global $width, $info ;
$fldNum = 0 ;
while ($fldNum < count($info)) {
printf("%-{$width[$fldNum]}.{$width[$f... $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 ;
$fldNum = 0 ;
while ($fldNum < count($headers)) {
printf("%-{$width[$fldNum]}.{$width[$f... $headers[$fldNum] ) ;
$fldNum++ ;
}
print "\n" ;
// print a line
$len = 0 ;
foreach ($width as $w) { $len += $w ; }
printf("%'-{$len}s\n", "") ;
}
?>