Testing Files

Before you work with a file or directory, you should learn more about it. PHP provides many functions that help you discover information about files on your system. This section briefly covers some of the most useful ones.

Checking for Existence with file_exists()

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

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

A File or a Directory?

You can confirm that the entity you are testing is a file, as opposed to a directory, with the is_file() function. is_file() requires ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.