PHP is_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, is_file() tells you whether a path points to a regular file. It returns true for a file and false for a folder, a missing path or anything else.

Use it before you read, include or delete a file. It is stricter than file_exists(), which also says true for folders.

Syntax

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

PHP
$exists = is_file('notes.txt');
CallWhat it does
is_file('notes.txt')Returns true if notes.txt exists and is a normal file.
is_file(__FILE__)Returns true because the current script is a file.
is_file('uploads')Returns false if uploads is a folder.

is_file() follows symbolic links, so a link that points to a real file also returns true. The answer can be cached for the rest of the script, so call clearstatcache() if the file changes while the script runs.

Library declarations
PHP
is_file(string $filename): bool

Parameters

ParameterDescription
filenameRequired. The path to check. It can be relative or absolute.

Return Value

Return ValueDescription
boolReturns true if the path exists and is a regular file, otherwise false.

PHP Examples

Example 1 — Check That a File Exists

Create a small file and confirm with is_file().

PHP
<?php
$file = sys_get_temp_dir() . '/is_file_demo.txt';
file_put_contents($file, 'Hello');

if (is_file($file)) {
    echo "It is a file", PHP_EOL;
}

unlink($file);

How It Works

  1. file_put_contents() creates a small text file in the temporary folder.
  2. is_file() finds the file and returns true.
  3. The message is printed.
  4. unlink() deletes the file again so nothing is left behind.

Example 2 — Folders and Missing Paths Return False

is_file() only accepts regular files. A folder and a path that does not exist both give false.

PHP
<?php
$dir = sys_get_temp_dir();

echo "Folder: ", is_file($dir) ? 'yes' : 'no', PHP_EOL;
echo "Missing: ", is_file($dir . '/no-such-file.txt') ? 'yes' : 'no', PHP_EOL;
echo "This script: ", is_file(__FILE__) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. The system temporary folder exists, but it is a directory, so is_file() returns false.
  2. A path to a file that is not there also returns false and gives no warning.
  3. __FILE__ is the path of the running script, which is a real file.
  4. Only the last check prints yes.

Example 3 — Tell Files and Folders Apart in a Listing

Scan a folder and use is_file() to label each item.

PHP
<?php
$dir = sys_get_temp_dir() . '/is_file_list';
mkdir($dir);
file_put_contents($dir . '/a.txt', 'x');
file_put_contents($dir . '/b.txt', 'x');
mkdir($dir . '/sub');

foreach (scandir($dir) as $item) {
    if ($item === '.' || $item === '..') {
        continue;
    }
    echo $item, ': ', is_file($dir . '/' . $item) ? 'file' : 'folder', PHP_EOL;
}

unlink($dir . '/a.txt');
unlink($dir . '/b.txt');
rmdir($dir . '/sub');
rmdir($dir);

How It Works

  1. A temporary folder is built with two files and one sub folder.
  2. scandir() lists every name, including the special entries . and .., which the loop skips.
  3. is_file() is true for the two text files and false for sub.
  4. The ternary operator prints file or folder, and the cleanup removes everything.

Frequently Asked Questions

It checks whether a path is a regular file and returns true or false.
file_exists() is true for files and folders. is_file() is true only for regular files.
No. A folder returns false. Use is_dir() to check for folders.
No. It only tells you that a regular file exists. Use is_readable() to find out whether PHP may read it.
PHP caches file information during a script. Call clearstatcache() if the file is created or deleted while the script is running.

Next: is_link()

is_link() tells you whether a path is a symbolic link (symlink). A symbolic link is a smal.

is_link() 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