Page 189 - Open Soource Technologies 304.indd
P. 189
Open Source Technologies
Notes Permission-related information is not all you might need to know about a file. The next section
shows how to determine the file size.
11.2.4 Determining File Size with filesize()
Given the path to a file, the filesize() function attempts to determine and return its size in bytes.
It returns false if it encounters problems.
echo “The size of test.txt is “.filesize (“test.txt”);
Finding the specific file size is important is situations where you want a attach a file to an email
or stream a file to the user—you’ll need to know the size so as to properly crate the headers
(in the case of the email) ro0 known when to stop sending bytes to the user (in the case of the
stream). For more general purposes, you might want to get the file size so that you can display
it to the user before she attempts to download some monstrous application or high-resolution
photograph from your site.
11.2.5 Getting Date Information About a File
Sometimes you need to know when a file was last written to or accessed. PHP provides several
function that can provide this information.
You can find out the last-accussed time of a file using the fileatime() function.
This function requires the file path and returns the date that the file was last accessed. To access a
file means either to read or write to it. Dates are returned from all the date information functions
in time stamp—that is, the number of seconds since January 1, 1970. The examples in this book
use the date() function to translate this value into human-readable form.
$atime = fileatime(“test.txt”);
Echo “test.txt was last accessed on *.date(“D d M Y g:I A”, Satime);
//Sam ple output: test.txt was last accessed on Sun 13 Jan 2008 5:33 AM.
You can discover the modification date of a file with the function filemtime(),
Which requires the file path and returns the date in UNIX epoch format. To modify a file means
to change its contents in some way.
$atime = filemtime (“test.txt”);
echo “test.txt was last modified on “.date(“D d M Y g:I A”, $mtime);
// Sample output; test.txt was last modified on Sun 13 Jan 2008 5:45 AM.
PHP also enables you to test the change time of a document with the filectime() function. On
UNIX systems, the change time is set when a file’s contents are modified or when changes are
made to its permissions or ownership. On other platforms, the filectime() returns the creation date.
$ctime= filectime (“text.txt”);
echo “test.txt was last changed on *.date (“D d M Y g:I A”, $ctime);
// Sample output; test.txt was last changed on Sun 13 Jan 2008 5:45.
184 LOVELY PROFESSIONAL UNIVERSITY