PHP Tutorial - PHP file_exists() Function






Definition

The file_exists() function checks whether or not a file or directory exists.

Syntax

PHP file_exists() Function has the following syntax.

file_exists(path)

Parameter

ParameterIs RequiredDescription
pathRequired.Path to check

Return

Returns TRUE if the file or directory specified by filename exists; FALSE otherwise.

Example

file_exists() returns true if the file exists and false otherwise.

We can use file_exists() to discover whether a file exists before attempting to open it:


<?PHP//from   ww w .ja v a  2s  .  c  o  m
      if (file_exists("data.txt")) {
         print "data.txt exists!\n";
      } else {
         print "data.txt does not exist!\n";
      }


      echo file_exists("data.txt");

?>

The code above generates the following result.