W3docs

is_file()

The is_file() function is a built-in PHP function that checks whether a given path is a regular file. This function returns true if the path is a regular file,

What is the is_file() Function?

is_file() is a built-in PHP function that tells you whether a path points to a regular file — an ordinary file on disk, as opposed to a directory, a symbolic link's target type, or a special file (a pipe, socket, or device node). It returns true only for regular files and false for everything else, including paths that do not exist.

This is the function you reach for before you try to read, include, or process a file, when "does this path actually point to a file I can work with?" matters more than "does this path exist at all?".

Syntax

is_file(string $filename): bool
  • $filename — the path to check. It can be relative (resolved against the current working directory) or absolute.
  • Return valuetrue if $filename exists and is a regular file, false otherwise. No warning is raised when the file is missing; you simply get false.

Basic Example

php— editable, runs on the server

Using __FILE__ (the absolute path of the current script) guarantees the check passes, so the script prints a message confirming the path is a regular file. Swap in any path you like — a missing path or a directory returns false.

is_file() vs file_exists() vs is_dir()

These three functions overlap, and choosing the wrong one is a common source of bugs:

FunctionReturns true for
is_file()regular files only
file_exists()files and directories
is_dir()directories only

The key trap: file_exists('/some/folder') is true for a directory. If you then try to fopen() or include it as a file, you get an error. Use is_file() whenever your next step assumes a real file:

<?php

$path = sys_get_temp_dir(); // a directory that definitely exists

var_dump(file_exists($path)); // bool(true)  — it exists
var_dump(is_file($path));     // bool(false) — but it is NOT a file
var_dump(is_dir($path));      // bool(true)  — it is a directory

Guarding a File Read

A typical real-world use: confirm the path is a usable file before reading it. Pairing is_file() with is_readable() avoids both "not a file" and "no permission" failures:

<?php

$path = __FILE__;

if (is_file($path) && is_readable($path)) {
    echo "Safe to read: " . basename($path);
} else {
    echo "Cannot read that path.";
}

This prints Safe to read: is-file.mdx-style output (the basename of the running script), and short-circuits cleanly if the path is a directory, is missing, or is not readable.

The stat cache gotcha

PHP caches the results of filesystem checks like is_file() for the duration of a request to avoid hitting the disk repeatedly. If a file is created or deleted after PHP has already checked the same path, a second is_file() call may return the stale, cached answer. When you need a fresh result — for example in a long-running script that creates a file and immediately re-checks it — clear the cache first:

<?php

clearstatcache();          // discard cached stat results
var_dump(is_file($path));  // now reflects the current state on disk

Common Pitfalls

  • It is not a security check. A path can pass is_file() and still be unreadable or unwritable. Combine it with is_readable() or is_writable() before acting.
  • Symbolic links are followed. is_file() checks the target of a symlink, not the link itself. A symlink pointing at a regular file returns true.
  • No warning on missing paths. Unlike opening a file, a nonexistent path just returns false, so is_file() is safe to call without suppressing errors.

Conclusion

is_file() answers one precise question — "is this a regular file?" — and is the right gate to place in front of any code that reads, includes, or processes a file. Reach for file_exists() when a directory would also be acceptable, is_dir() when you specifically want a folder, and pair is_file() with the readability checks above when permissions matter.

Practice

Practice
What does the PHP 'is_file()' function do?
What does the PHP 'is_file()' function do?
Was this page helpful?