PHP file_exists() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, file_exists() checks whether a file or a folder exists at the given path. It returns true if it does and false if it does not.

Use it as a quick safety check before you read, include or delete something, so your script does not fail with a warning. It is one of the most used functions when working with files.

Syntax

Call file_exists() with the values you need. Start with this line.

PHP
$exists = file_exists('data.txt');
CallWhat it does
file_exists('data.txt')Returns true if data.txt exists as a file or a folder.
file_exists(__DIR__ . '/config.php')Checks a path built from the current script folder, which is safer than a relative path.
file_exists('uploads')Also returns true for folders, not only for files.

file_exists() returns true for both files and directories. Use is_file() when you only want files and is_dir() when you only want folders. PHP caches the result, so call clearstatcache() if the file may change while the script runs.

Library declarations
PHP
file_exists(string $filename): bool

Parameters

ParameterDescription
filenameRequired. The path to a file or directory. It can be absolute or relative.

Return Value

Return ValueDescription
booltrue if the file or directory exists, otherwise false.

PHP Examples

Example 1 — Check If a File Exists

Check the current script, which always exists, and a file that does not exist.

PHP
<?php
$real = file_exists(__FILE__);
$missing = file_exists('missing-file.txt');

echo 'This script exists: ', var_export($real, true), PHP_EOL;
echo 'Missing file exists: ', var_export($missing, true), PHP_EOL;

How It Works

  1. __FILE__ holds the full path of the script that is running.
  2. file_exists() returns true for it because the script is there.
  3. The second path points to a file that was never created, so the result is false.
  4. var_export() prints true and false as words instead of 1 and an empty string.

Example 2 — Folders Count Too

file_exists() says true for a directory. Use is_file() when you only want to accept real files.

PHP
<?php
echo 'Directory exists: ', var_export(file_exists(__DIR__), true), PHP_EOL;
echo 'Is it a file: ', var_export(is_file(__DIR__), true), PHP_EOL;

How It Works

  1. __DIR__ is the folder that contains the running script.
  2. file_exists() returns true because that folder exists.
  3. is_file() returns false because a folder is not a regular file.
  4. Pick the function that matches what you really need to check.

Example 3 — Create a File Only If Missing

A common pattern is to check for a file first and create it only when it is not there yet.

PHP
<?php
$path = sys_get_temp_dir() . '/codetofun_file_exists.txt';
@unlink($path);

if (!file_exists($path)) {
    echo 'File missing, creating it', PHP_EOL;
    file_put_contents($path, 'Hello');
}

echo 'File exists now: ', var_export(file_exists($path), true), PHP_EOL;

unlink($path);

How It Works

  1. unlink() makes sure the demo file does not exist before the test starts.
  2. !file_exists() is true, so the code inside the if block runs.
  3. file_put_contents() creates the file with the text Hello.
  4. The second file_exists() call now returns true.

Frequently Asked Questions

It checks whether a file or directory exists at the path you give. It returns true if it exists and false if it does not.
Yes. It returns true for both files and directories. Use is_file() or is_dir() if you need to tell them apart.
file_exists() accepts files and directories, while is_file() returns true only for regular files.
Usually the path is wrong, for example a relative path that depends on the current working directory. It can also be a permission problem on a parent folder. Try an absolute path built with __DIR__.
Only when the file can be created or deleted while your script runs. PHP caches the result, and clearstatcache() clears that cache so you get a fresh answer.

Next: file_get_contents()

file_get_contents() reads a whole file and returns its content as one string. It is the si.

file_get_contents() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful