Content · Programming · PHP

PHP file_exists() Function: Usage and Examples

Checks whether a filesystem path points to an existing entry.

1 views Published Oct 17, 2026 Updated Oct 17, 2026

file_exists() Function is one of the PHP operations used in filesystem and stream workflows. Checks whether a filesystem path points to an existing entry. The examples below show what it does and how to handle its result.

What Is file_exists() Function?


Checks whether a filesystem path points to an existing entry.

Its syntax is:

file_exists(string $filename)

The parameters are:

  • string $filename — The source or destination filesystem path.

Return value: The declared return type is bool. It returns true for directories as well as files; use is_file() when necessary.

Important: It returns true for directories as well as files; use is_file() when necessary.

Example 1: Basic Usage


The basic example focuses on the behavior of file_exists() Function: checks whether a filesystem path points to an existing entry. Inputs are defined locally or shown as application-controlled paths.

<?php

var_dump(file_exists(__FILE__));
var_dump(file_exists(__DIR__ . "/not-created-file.txt"));

The displayed value, file content or return status depends on the input and surrounding environment. Results involving local paths, identifiers or generated files are not fixed values.

Example 2: A Different Use Case


This second example illustrates another input or execution condition for file_exists() Function. Notice the different checks, arguments, or output handling.

<?php

$path = __DIR__ . "/cache";
if (file_exists($path) && is_dir($path)) { echo "Cache directory exists"; }

This variation matters because it returns true for directories as well as files; use is_file() when necessary.

Practical Example: Using the Operation in an Application


The following example demonstrates how file_exists() Function fits into a more complete file or archive workflow, including inputs and appropriate checks.

<?php

$configFile = __DIR__ . "/settings.ini";
if (!file_exists($configFile)) { echo "Create initial configuration"; } else { echo "Configuration found"; }

The operation shown is an illustrative script. Adapt the filenames, required permissions, and error handling to your own controlled application environment before running it.

Important Behavior and Edge Cases


  • Function behavior: Checks whether a filesystem path points to an existing entry.
  • Limitation: It returns true for directories as well as files; use is_file() when necessary.

Comparison With Related Functions


Function or MethodDifference
file_exists() FunctionChecks whether a filesystem path points to an existing entry.
is_file()Checks for regular files, while file_exists() also returns true for directories.

Use file_exists() Function for the specific behavior described above. The related operation, is_file(), serves a different purpose; do not substitute it unless that different behavior is intended.

Common Beginner Mistakes


MistakeCorrect Approach
Assuming this operation has the same behavior on every file or environment.It returns true for directories as well as files; use is_file() when necessary. Check prerequisites and return values before using the result.
Treating is_file() as an interchangeable replacement for file_exists() Function.Checks for regular files, while file_exists() also returns true for directories.

Key Takeaways


  • Checks whether a filesystem path points to an existing entry.
  • It returns true for directories as well as files; use is_file() when necessary.
  • Related operation: is_file() — Checks for regular files, while file_exists() also returns true for directories.

file_exists() Function is useful when its specific behavior matches your application requirement. Keep paths controlled, check failures, and handle environment-dependent operations deliberately.

Share this resourceSend this page to someone who may find it useful.