PHP file_exists() Function
The file_exists() function in PHP is a built-in function used to check whether a file or directory exists at a specified path. You can use this function to verify the existence of a file before processing it, which helps prevent errors.
You can also use the file_exists() function when you want to create a new file and want to ensure that the file does not already exists on the web server, thereby avoiding accidental overwriting.
Basic Syntax of file_exists() Function in PHP
The file_exists() function has the following syntax:
bool file_exists(string $file_name)Parameters:
The file_exists() function accepts one parameter:
- $file_name: This parameter specifies the path to the file or directory that you want to check. It can be an absolute path (/var/www/html/file.txt) or a relative path (file.txt).
Return Value:
- The file_exists() function returns true if the file or directory exists.
- Otherwise, returns false if the file or directory does not exist.
Example 1: Checking if a File Exists
<?php
$filename = "myfile.txt";
// Checking the existence of a file at a specified location.
if (file_exists($filename)) {
echo "The file $filename exists.";
} else {
echo "The file $filename does not exist.";
}
?>Output:
The file myfile.txt exists.
In this example, if myfile.txt is present in the same folder as the PHP script, it will print “The file example.txt exists” on the console. Otherwise, it will print “The file example.txt does not exist”.
Example 2: Checking a Directory
<?php
$dirname = "uploads";
if (file_exists($dirname)) {
echo "The directory $dirname exists.";
} else {
echo "The directory $dirname does not exist.";
}
?>Output:
The directory uploads exists.
Note: The file_exists() function can also works with directories.
Example 3: Prevent Errors Before Opening a File
Instead of directly opening a file, you can first check whether it exists or not to avoid errors. This helps prevent PHP from throwing warnings if the file doesn’t exist. Look at the code below.
<?php
$filename = "myfile.txt";
if (file_exists($filename)) {
$content = file_get_contents($filename);
echo "File content: " . $content;
} else {
echo "Error: File not found!";
}
?>Output:
File content: Hello PHP!
Example 4: File Upload Validation
When users upload files, you may check if a file with the same name already exists in the directory. This prevents overwriting existing files during upload. Look the following code.
<?php
$uploadDir = "uploads/";
$file = $uploadDir . basename($_FILES["file"]["name"]);
if (file_exists($file)) {
echo "Sorry, file already exists.";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], $file);
echo "File uploaded successfully!";
}
?>Case 1: Suppose a user uploads a file named myfile.txt, and the same file already exists in the uploads/ folder or directory. In this case, file_exists($file) returns true. The output will be:
Output:
Sorry, file already exists.
Case 2: Suppose a user uploads a file named resume.pdf, and there is no file with this name in the uploads/ folder or directory. In this case, file_exists($file) returns false. PHP will move the uploaded file from the temporary folder to the uploads/ folder. The output will be:
Output:
File uploaded successfully!
Example 5: Looping through Multiple Files
You can check multiple files at once in a loop.
<?php
$files = ["index.php", "about.php", "contact.php"];
foreach ($files as $file) {
if (file_exists($file)) {
echo "$file exists. \n";
} else {
echo "$file is missing. \n";
}
}
?>Output:
index.php exists.
about.php exists.
contact.php exists.
Conclusion
The file_exists() function provided by PHP is used to check whether a file or directory is available before using it. It helps prevent runtime errors. This function is widely used in file handling, file uploads, backup system, and configuration files.
By combining it with functions like is_file(), is_dir(), and is_readable(), you can write more reliable and error-free file handling code.




