Validating Files


Before you work with a file or directory within your code, it's often a good idea to learn more about it, and whether it actually exists is a pretty good start! PHP provides many functions to help you to discover information about files on your system. This section briefly covers some of the most useful functions.

Checking for Existence with file_exists()

You can test for the existence of a file with the file_exists() function. This function requires a string representation of an absolute or relative path to a file, which might or might not be present. If the file is found, the file_exists() function returns true; otherwise, it returns false.

if (file_exists("test.txt")) {         echo "The file exists!"; }


This is all well and good, but what if you're unsure whether something is a file or a directory, and you really need to know? Read on!

A File or a Directory?

You can confirm that the entity you're testing is a file, as opposed to a directory, using the is_file() function. The is_file() function requires the file path and returns a Boolean value.

if (is_file("test.txt")) {         echo "test.txt is a file!"; }


Conversely, you might want to check that the entity you're testing is a directory. You can do this with the is_dir() function. is_dir() requires the path to the directory and returns a Boolean value.

if (is_dir("/tmp")) {         echo "/tmp is a directory"; }


After you know a file or directory exists, you may need to test its permissions. You'll learn about this in the next section.

Checking the Status of a File

When you know that a particular entity exists, and it's what you expect it to be (either a directory or a file), you'll need to know what you can do with it. Typically, you might want to read, write to, or execute a file. PHP can help you determine whether you can perform these operations.

The is_readable() function tells you whether you can read a file. On UNIX systems, you might be able to see a file but still be barred from reading its contents because of its user permissions. The is_readable() function accepts the file path as a string and returns a Boolean value.

if (is_readable("test.txt")) {         echo "test.txt is readable"; }


The is_writable() function tells you whether you have the proper permission to write to a file. As with is_readable(), the is_writable() function requires the file path and returns a Boolean value.

if (is_writable("test.txt")) {         echo "test.txt is writable"; }


The is_executable() function tells you whether you can execute the given file, relying on either the file's permissions or its extension, depending on your platform. The function accepts the file path and returns a Boolean value.

if (is_executable("test.txt")) {         echo "test.txt is executable"; }


Permission-related information isn't all you might need to know about a file. The next section shows how to determine the file size.

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 in situations where you want to attach a file to an email or stream a file to the useryou'll need to know the size so as to properly create the headers (in the case of the email) or know when to stop sending bytes to the user (in the case of the stream). For more general purposes, you may 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.

Getting Date Information About a File

Sometimes you need to know when a file was last written to or accessed. PHP provides several functions that can provide this information.

You can find out the last-accessed 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 stampthat is, the number of seconds since January 1, 1970. In our examples, we 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", $atime); // Sample output: test.txt was last accessed on Tue 14 Mar 2006 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.

$mtime = 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 Tue 14 Mar 2006 5:47 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("test.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 Tue 14 Mar 2006 5:47





Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net