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/*  w  w  w . ja  v a 2 s.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.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions