PHP file_exists( ) Function (original) (raw)

Last Updated : 11 Jul, 2025

The file_exists() function in PHP checks whether a file or directory exists on the server. It returns a boolean value:

**Syntax:

file_exists($path)

**In this syntax:

**Return Value:

**Example Usage of file_exists()

Let's take a simple example that checks if a file exists using file_exists().

PHP `

`

**In this example:

**Checking a Directory

The file_exists() function also works with directories. Here's an example that checks if a directory exists:

PHP `

`

**In this example:

**Conclusion

The file_exists() function in PHP is a powerful tool for verifying the existence of files or directories before performing operations on them. It helps prevent errors by ensuring that files are available for reading, writing, or manipulation, and can be used in conjunction with other functions like is_file() and is_dir() to get more detailed information about the file system. Understanding how to properly use file_exists() will make your file-handling scripts more robust and error-free.