PHP file_exists Function
last modified April 3, 2025
The PHP file_exists function checks whether a file or directory
exists. It's essential for file operations to avoid errors when accessing
nonexistent files.
Basic Definition
The file_exists function checks if a file or directory exists at
the specified path. It returns true if the file/directory exists, false otherwise.
Syntax: file_exists(string $filename): bool. The function works
with both files and directories, and accepts relative and absolute paths.
Basic file_exists Example
This shows the simplest usage of file_exists to check a file.
<?php
declare(strict_types=1);
$file = "example.txt";
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
This checks if "example.txt" exists in the current directory. The function returns a boolean that we use in a conditional statement.
Checking Absolute Path
file_exists works with absolute paths to specific locations.
<?php
declare(strict_types=1);
$file = "/var/www/html/config.ini";
if (file_exists($file)) {
echo "Configuration file found";
} else {
echo "Configuration file missing";
}
Here we check for a file at an absolute path. This is useful when you need to verify system files or files at specific locations.
Checking Directory Existence
The function can also check if a directory exists, not just files.
<?php
declare(strict_types=1);
$dir = "uploads";
if (file_exists($dir) && is_dir($dir)) {
echo "The directory $dir exists";
} else {
echo "The directory $dir does not exist";
}
We combine file_exists with is_dir to specifically
check for directories. This ensures we're checking a directory, not a file.
URL Check Example
file_exists doesn't work with HTTP URLs - it's for local files only.
<?php
declare(strict_types=1);
$url = "https://example.com/image.jpg";
if (file_exists($url)) {
echo "This will never be true for HTTP URLs";
} else {
echo "Use file_get_contents or cURL for remote files";
}
This demonstrates that file_exists only works with local filesystem
paths. For URLs, you need different functions like file_get_contents.
Permission Considerations
File permissions can affect file_exists results.
<?php
declare(strict_types=1);
$restricted = "/etc/shadow";
if (file_exists($restricted)) {
echo "File exists but you may not have access";
} else {
echo "File may exist but is inaccessible";
}
Even if a file exists, PHP might not have permission to access it. The function may return false for files the web server user can't read.
Best Practices
- Combine Checks: Use with is_file/is_dir for specific checks.
- Error Handling: Implement proper error handling around file ops.
- Security: Validate and sanitize file paths before checking.
- Caching: Be aware PHP may cache file_exists results.
- Performance: Avoid excessive checks in loops.
Source
This tutorial covered the PHP file_exists function with practical
examples showing its usage for checking file and directory existence.