
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
file_exists() function in PHP
The file_exists method check whether a file or directory exists or not. It accepts the path of the file or directory to be checked as the parameter. The following are its uses −
It is useful when you need to know whether a file exists or not before processing.
With that, use this function while creating a new file to know whether the file already exists or not.
Syntax
file_exists($file_path)
Parameters
file_path − Set the path of file or directory to be checked for existence.Required.
Return
The file_exists() method returns.
- True, if the file or directory exists
- False, if the file or directory does not exist
Example
Let us see an example that checks for “candidate.txt” file and display a message even if the file does not exist.
<?php $myfile = '/doc/candidate.txt'; if (file_exists($myfile)) { echo "$myfile exists!"; } else { echo "$myfile does not exist!"; } ?>
The following is the output showing the file does not exist.
Output
/doc/candidate.txt does not exist!
- Related Articles
- file() function in PHP
- Check if a File exists in C#
- Check if a file exists in Java\n
- Determine if file or directory exists in Java
- PHP file://
- How to check if a file exists in Golang?
- How to check if a file exists in Perl?
- Check whether property exists in object or class with PHP
- How to check if a file exists or not in Java?
- How to check if a File Type Exists in a Directory?
- How to create a new image from a JPEG file using the imagecreatefromjpeg() function in PHP?
- strcmp() function in PHP
- strcoll() function in PHP
- strcspn() function in PHP
- strip_tags() function in PHP
- stripcslashes() function in PHP

Advertisements