O'Reilly logo

Sams Teach Yourself PHP in 24 Hours, Third Edition by Matt Zandstra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required