PHP Check If A File Exists

In this tutorial, we are going to learn the method that can help to search for the files and folders in the directory, we will see how to check the existence of a file or a directory through PHP so let’s get started with the tutorial.

As we all know that PHP is a versatile language it offers a bunch of outstanding methods and classes and each method or class performs a specific task, as it is PHP provides a method that helps to find an existing and non-existing file or directory in the system. To check the existence of a file or a folder we can use the file_exist() method of PHP, take a look at the method described below.

PHP file_exists() Method

The PHP file exists() is a built-in method in PHP, it checks whether a file or folder does exist or not of the specified path or name, it returns a Boolean value true or false, if the file exists so it returns true otherwise it returns false.

This method requires only one parameter which is the correct path or the correct name of that specific file or folder. We pass the file path or name as a parameter in the file_exists() method and then it searches for that file in that defined path, name. After finishing the searching process it returns a boolean value. 

For better understanding check out the example below.

Example 1


// declaring variable 
$file = 'register.php';

Using file_exists() method below

if (file_exists($file))
           echo "The file $file exists";
           echo "The file $file does not exist";


Code Explanation

In the above code, we defined a variable “file” and assigned it the file name whose existence we want to check.

Then we used the if condition with the file_exists() function and passed our file variable as a parameter to it.

The function will check the existence of the specified file and the if condition will print a message “file exists” if it gets a result else it will print the “file does not exist” message.

Let’s see the results below.


The file register.php exists

As you can see the output above it printed the file exists message which means our function worked fine.

We can use two more functions with the file_exists() function in order to get more information regarding the specified file, By using the is_readable() and is_writable() functions we can effortlessly check if the required existing file is also read or writable or not. Try the following example to have a clear concept.

Example 2


$file = "register.php";

if(is_readable($file)) {
    echo 'The file "'.$file.'" is readable .';
echo "<br>";

if(is_writable($file)) {
    echo 'The file is writable we can make changes in "'.$file.'".';

Code Explanation

In the above code, we declared a variable and assigned it the filename which we want to verify after that we used the is_readable() and is_writable() functions with the if condition that will print a message if it finds the file read or writable, check out the results below.


The file “register.php” is readable.

The file is writable we can make changes in “register.php”.


In this tutorial we discussed the method to check the file existence in PHP, firstly we saw an introduction to the file_exists() method, and then we saw an example and explained the code.

We also talked about the is_readable and is_writable() functions of PHP and practiced their examples, we understood their code explanations, and at the end, we saw their outputs as well. 

That’s all for today hope you like the tutorial if you have any queries regarding this article so feel free to contact us below. Thanks for reading.

Leave a Comment