PHP File Exists

Summary: in this tutorial, you will learn how to check if a file exists in PHP using the file_exists(), is_file(), is_readable(), and is_writable() functions.

PHP provides some useful functions that allow you to check if a file exists. Let’s examine these functions and how to use them effectively.

Check if a file exists using the file_exists() function

To check if a file exists, you use the file_exist() function:

file_exists ( string $filename ) : bool
Code language: PHP (php)

The file_exists() function accepts a filename and returns true if the file exists; otherwise it returns false.

The following example uses the file_exists() function to check if the file readme.txt exists in the current directory:

<?php $filename = 'readme.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message;
Code language: HTML, XML (xml)

If the readme.txt exists in the same directory of the script, you’ll see the following message:

The file readme.txt exists
Code language: CSS (css)

…otherwise, you’ll see a different message:

The file readme.txt does not exist
Code language: CSS (css)

Note that the $filename can be also a path to a directory. In this case, the file_exists() function returns true if the directory exists.

Check if a file exists using the is_file() function

If you want to check if a path is a file (not a directory) and exists in the file system, you can use the is_file() function.

The is_file() function accepts a $filename and returns true if the $filename is a file and exists:

is_file ( string $filename ) : bool
Code language: PHP (php)

The following example uses the is_file() function to check if the file readme.txt exists:

<?php $filename = 'readme.txt'; if (is_file($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message;
Code language: HTML, XML (xml)

Check if a file exists and readable

In practice, you often want to check if a file exists before reading its contents. To check if a file exists and is readable, you use the is_readable() function:

is_readable ( string $filename ) : bool
Code language: PHP (php)

The is_readable() function returns true if the $filename exists and readable, or false otherwise. Note that the $filename can be a directory.

The following example uses the is_readable() function to check if the readme.txt file exists and readable:

<?php $filename = 'readme.txt'; if (is_readable($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message;
Code language: HTML, XML (xml)

Check if a file exists and writable

Before writing to a file, you need to check the file exists and is writable. In this case, you can use the is_writable() function:

is_writable ( string $filename ) : bool
Code language: PHP (php)

The is_writable() function returns true if the $filename exists and writable, or false otherwise.

The following example uses the is_writable() function to check the readme.txt file exists and writable:

<?php $filename = 'readme.txt'; if (is_writable($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message;
Code language: HTML, XML (xml)

Summary

  • Use the file_exists() function to check if a file exists.
  • Use the is_file() function to check if a path is a regular file, not a directory, and that file exists.
  • Use the is_readable() function to check if a file exists and readable.
  • Use the is_writable() function to check if a file exists and writable.
Did you find this tutorial useful?