How to Use PHP’s is_file() Function to Check File Existence

This article explains the PHP is_file() function, its syntax, parameters, return values, and provides clear code examples for checking whether a path points to an existing regular file and how to differentiate it from directories using is_dir().

php Courses
php Courses
php Courses
How to Use PHP’s is_file() Function to Check File Existence

In PHP programming, the is_file() function is a very useful built‑in function that determines whether a given path exists and is a regular file.

First, the syntax of is_file() is: bool is_file ( string $filename ) The function accepts a single argument $filename, which specifies the path to be checked. It returns true if the path points to an existing regular file; otherwise it returns false.

Below is a simple example demonstrating how to use is_file() to test if a file exists:

<?php
$file = "/path/to/file.txt";

if (is_file($file)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}
?>

In this example we define a file path $file and then call is_file(). If the file is present, the script outputs “File exists!”; otherwise it outputs “File does not exist!”.

Besides checking for file existence, is_file() can also be used to verify that a path is a regular file. If the path points to a directory or a special file (such as a device file or a symbolic link), is_file() will return false.

Here is another example that shows how to determine whether a given path is a regular file:

<?php
$path = "/path/to/directory";

if (is_file($path)) {
    echo "This is a regular file!";
} else {
    echo "This is not a regular file!";
}
?>

In this case we set $path to a directory path and use is_file(). Because the path is a directory, the function returns false and the script prints “This is not a regular file!”.

It is important to note that is_file() only checks for regular files. To test whether a path is a directory, you should use the is_dir() function instead.

In summary, the is_file() function is a practical PHP utility for verifying that a path exists and is a regular file; understanding its usage helps developers handle file‑system checks more reliably in their applications.

Additional resources (in Chinese) for learning various programming languages are provided below:

Java learning materials

C language learning materials

Frontend development learning materials

C++ learning materials

PHP learning materials

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPFilesystemis_filefile-existence
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.