PHP File_Exists() Function - Check If File Exists PHP
A look at how we can use the file_exists() function in PHP to check if a file or directory exists
We’re going to take a look at how we can use the file_exists() function in PHP to check if a file or directory exists in use cases such as before we try to read or write to them. This is a built-in function that is included in the PHP core from version 4 onward.
PHP File_Exists() Function Introduction & Syntax
The PHP file_exists() function is a built-in function that checks whether a given file or directory exists. The main difference between this function and the is_file() function is that the file_exists() function returns true for both files and directories while the is_file() function only returns true for files.
Parameters For PHP File_Exists Function
The filename parameter is mandatory.
This is the name of the file or directory we want to check. The function expects this to be a string
Return Value For PHP File_Exists Function
This function returns a boolean value: true if the file exists and is a regular file or a directory, or returns false otherwise. The results produced from this function are cached.
Quick Notes About The PHP File_Exists Function:
If you want to open a specific file you should use the is_file() function instead as this function will return false for directories. This function will check whether a given symbolic link exists by checking if the target of the link exists. You can use this function with url wrappers such as http:// and ftp:// if the wrappers have been enabled. This function is case-sensitive on case-sensitive file systems and case-insensitive on case-insensitive file systems. You can use stream_get_wrappers() to check what url wrappers have been enabled on your server.
Commonly Seen Errors and Exceptions
If the filename parameter is an empty string this function will return false and return an E_WARNING level error. If you are trying to access a remote url and not a directory or file that is local you can return false be returned when the file exists. It’s best to use FTP.
Platforms using 32bit integers might return poor results for filesystem functions such as this one on files larger than 2 gigabytes. This is due to PHP’s integer type being signed.
PHP Function File_Exists() Examples
Let’s look at a number of examples of how we can use the file_exists() function to check if a file or directory exists in PHP.
Simple PHP File_Exists() Example To Check If Directory Exists
This code example performs a simple check to see if a directory exists on the server. Remember to change /folder1/folder2/ to an actual directory on your file system.
If the directory does not exist you’ll see the following output: “The directory does not exist”, otherwise you know the directory exists. The file_exists() function is very useful for checking user input during form submission processes to make sure an image or document exists before trying to process it. You can get some strange errors and broken links if you don’t perform these types of checks first.
Check If File Exists PHP
This code example checks to see if a text file exists in your directory. Remember to change /myFolder/myfile.txt to the actual file in your file system and update "/" based on your operating system.
If the file does not exist you’ll see the following echo output: “The file does not exist”, otherwise you know the text file exists and your PHP code can access it based on the file permissions. Just like with directories, checking if a file exists using file_exists() is a great way to handle user input errors. You don’t want to try and process a file that doesn’t exist or cannot be located in the provided directory!
PHP Overwrite File If It Exists
This example will overwrite a file if it already exists in the provided directory. Remember to change /folder1/folder2/ to the actual directory on your file system and update "/" based on your operating system.
If the text file ‘test.txt’ exists in the provided directory it will be deleted and replaced with a new empty text file. You could put other logic in this code snippet to write to the file instead of unlink() which deletes the file.
A Real File_Exists() Usecase
PIM software tools often need to access product catalog information from an internal database to integrate customer data into the software tools dashboard. These files are normally massive in size and the data workflow to pull them into the software system takes time. Considering how long this process can take, it’s poor UX to make users wait the entire time to find out the system doesn’t have access to the files required. This function is a quick way to provide UX feedback that the files required are not accessible, and which files those are.
Deeper Examples Of File_Exists() Function
Let’s look at some powerful and efficient ways to use the file_exists() function.
Validate An Image Before Upload in File System
In this first “deeper” example, we’re going to look at how we can validate whether or not an image exists before uploading it. We’ll use the file_exists() function for this task.
We use the file_exists() function to check if the file already exists in our "uploads" directory. If it does, we echo "Sorry, file already exists." and set $uploadOk to 0.
PHP File Exists With a Relative Path
Here's how we can use file_exists() to check if a text file exists when given a relative path.
This is especially useful for larger applications where you might want to keep your paths relative for portability reasons. Cloud-based applications come to mind where you might have different servers with different file systems.
Check if remote file exists before unlink
Apart from checking if the file exists on a local server in a specified path, we might need to check if a remote file exists before we decide to delete it.
Here we’re checking if the logo.png file exists in the $_SERVER['DOCUMENT_ROOT']."/folder/test.txt" location. If it does, we use the unlink() function to delete the file. We can also use the unlink() function on other file types deployed in a remote setting.
php file_exists vs is_file
The key differences between file_exists and is_file are:
file_exists can also check whether a directory exists while is_file can only check whether a file exists.
file_exists is faster than is_file when used to check whether a large number of files exist. In general, is_file should be used only if it is really important to know whether a given path is a regular file.
File_Exists() is a great way to check if a given file exists before attempting to perform operations on it. This function can save us time and resources by letting us know if we can move forward with file operations or not. This function is also great for security purposes as we can check if certain sensitive files exist before allowing a user access to non-existing files.