How to check if File Exists in PHP?

Verify if a File exists with PHP

In PHP, function to check if a file exists in a folder path is file_exists.

Just pass the file with complete path to this function. If the file is present, then this function will return True. Otherwise you will get a False as return code.

Here is a sample code snippet.

$pathtofile = '/folder1/subfolder1/filename.ext';

//Returns True or False
echo file_exists($pathtofile);

//Handle Return code in If Condition
if (file_exists($pathtofile)) {
   echo "File found in directory path";} 
else {
   echo "File not found in the path";}

The return code can be printed or assigned to a Boolean variable and used inside your php code.

External Reference: Here is another link to this PHP file_exists function.