How to Use PHP is_file() to Check File Existence and Type
This article explains the PHP is_file() function, covering its syntax, parameters, return values, and provides clear code examples for checking whether a path exists, is a regular file, and how to differentiate files from directories using is_dir().
In PHP programming, the is_file() function is a useful tool for determining whether a given path exists and is a regular file. This article introduces its syntax, parameters, return values, and provides several code examples.
Syntax: bool is_file ( string $filename ) The function accepts a single argument $filename representing the path to check and returns true if the path points to an existing regular file, otherwise false.
Example 1 – checking a file’s existence:
<?php
$file = "/path/to/file.txt";
if (is_file($file)) {
echo "文件存在!";
} else {
echo "文件不存在!";
}
?>This script defines $file, calls is_file(), and outputs a message indicating whether the file exists.
Beyond existence, is_file() also distinguishes regular files from directories or special files; it returns false for those.
Example 2 – verifying a path is a regular file:
<?php
$path = "/path/to/directory";
if (is_file($path)) {
echo "这是一个普通文件!";
} else {
echo "这不是一个普通文件!";
}
?>Here $path is checked; the script prints a message based on whether the path is a regular file.
Note: to test for directories, use is_dir() instead of is_file().
In summary, is_file() is a practical PHP function for file‑system checks, and understanding its usage helps developers write more robust backend code.
Java learning material download
C language learning material download
Frontend learning material download
C++ learning material download
PHP learning material download
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
php Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
