Backend Development 3 min read

Master PHP’s is_file(): Check Files and Paths Like a Pro

This article explains how the PHP is_file() function works, shows its syntax, and provides clear code examples for checking whether a given path exists and is a regular file, while also noting its limitations and the alternative is_dir() function.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Master PHP’s is_file(): Check Files and Paths Like a Pro

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

Syntax

<code>bool is_file ( string $filename )</code>

The function accepts a single parameter $filename , the path to be checked, and returns true if the path points to an existing regular file, otherwise false .

Simple example

<code>&lt;?php
$file = "/path/to/file.txt";

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

This script defines a file path in $file and uses is_file() to test its existence, outputting a corresponding message.

Checking a path for a regular file

<code>&lt;?php
$path = "/path/to/directory";

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

Here $path is examined; is_file() returns false for directories or special files.

Important note

The is_file() function only checks for regular files. To test whether a path is a directory, use the is_dir() function instead.

In summary, is_file() is a practical PHP function for verifying the existence and type of a file, and the examples above demonstrate its usage in real‑world development.

backend developmentPHPTutorialfile handlingis_file
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

login 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.