PHP file_exists() | Check if File or Directory Exists

The PHP file_exists() function is used when we need to check whether a file or a directory exists or not. For example:

   $x = file_exists("codescracker.txt");
      echo "<p>The specified file is available.</p>";
      echo "<p>The specified file is not available.</p>";

The output of above PHP example on file_exists() function is:

php file_exists function

Since the file codescracker.txt is available in the current directory, therefore the above example produces the output that you have seen.

PHP file_exists() Syntax

The syntax of file_exists() function in PHP, is:


PHP file_exists() Example

   $file = "codescracker.txt";
      echo "<p>The file, <b>$file</b> exists.</p>";
      echo "<p>The file, <b>$file</b> does not exists.</p>";
   $path = "C:\Users\DEV\";
      echo "<p>The path, <b>$path</b> exists.</p>";
      echo "<p>The path, <b>$path</b> does not exists.</p>";

In above program, both the file and the path exists, therefore the output produced by above PHP example should be:

php file exists example

PHP Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube