Backend Development 4 min read

Using PHP file_exists() to Check Files and Directories

This article explains the PHP file_exists() function, its syntax, parameters, return values, provides example code for checking both files and directories, and lists important usage notes and best practices for reliable filesystem operations.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP file_exists() to Check Files and Directories

In PHP programming, the file_exists() function is used to determine whether a specified file or directory exists.

Syntax:

bool file_exists ( string $filename )

Parameter:

$filename : the path of the file or directory to check.

Return value: Returns true if the file or directory exists, otherwise false .

Example: The following code demonstrates checking both a file and a directory.

The example first defines a file path variable $file and uses file_exists() to verify its existence, outputting a corresponding message.

It then defines a directory path variable $dir and uses both file_exists() and is_dir() to confirm the directory’s presence before printing the result.

Notes:

The function can check local files, directories, or remote URLs.

It does not differentiate between files and directories; both return true when they exist.

It only reports existence, not accessibility; consider using is_readable() or is_writable() for permission checks.

Summary: file_exists() is a fundamental PHP function for verifying the existence of files or directories, returning a boolean value, and can be combined with other file‑handling functions to build robust file system operations.

php8,我来也

扫描二维码免费领取学习资料

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