file_exists() / is_file() / is_dir() Since: PHP 4(2000)
Checks whether a file or directory exists, its type, and its permissions. These functions are used to verify safety before performing file operations.
Syntax
// Returns true if the file or directory exists. file_exists($filename); // Returns true if the path is a regular file. is_file($filename); // Returns true if the path is a directory. is_dir($filename); // Returns true if the file is readable. is_readable($filename); // Returns true if the file is writable. is_writable($filename);
Function List
| Function | Description |
|---|---|
| file_exists($filename) | Returns true if the file or directory exists. Symbolic links are also included. |
| is_file($filename) | Returns true if the path is a regular file. Directories and symbolic links return false. |
| is_dir($filename) | Returns true if the path is a directory. Regular files return false. |
| is_readable($filename) | Returns true if the file exists and the current user has read permission. |
| is_writable($filename) | Returns true if the file exists and the current user has write permission. is_writeable() is an alias. |
Return Value
All functions return a boolean. They return true if the condition is met, or false otherwise. Results are cached by PHP, so repeated calls for the same file will return the cached result.
Sample Code
<?php
// Check that the file exists before reading it.
$path = "/var/www/html/config.json";
if (file_exists($path)) {
$config = file_get_contents($path);
echo "Config file loaded.";
} else {
echo "Config file not found.";
}
// Use is_file() to distinguish files from directories.
echo var_export(is_file("/var/www/html/index.php"), true); // Outputs 'true' because it's a file.
echo var_export(is_file("/var/www/html/"), true); // Outputs 'false' because it's a directory.
// Use is_dir() to check for a directory.
$upload_dir = "/var/www/html/uploads";
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true); // Create the directory if it doesn't exist.
echo "Upload directory created.";
}
// Check permissions before performing file operations.
$log_file = "/var/www/html/app.log";
if (is_writable($log_file)) {
file_put_contents($log_file, date("Y-m-d H:i:s") . " log\n", FILE_APPEND);
} else {
echo "No write permission for the log file.";
}
// Check read permission for the config file.
$config_path = "/var/www/html/pvt/common.php";
if (is_readable($config_path)) {
echo "Config file is readable.";
} else {
echo "No read permission for the config file.";
}
// Use clearstatcache() to clear the stat cache.
file_put_contents("/var/www/html/test.txt", "test");
echo var_export(file_exists("/var/www/html/test.txt"), true); // Outputs 'true'.
unlink("/var/www/html/test.txt");
clearstatcache(); // Without clearing the cache, the old result would be returned.
echo var_export(file_exists("/var/www/html/test.txt"), true); // Outputs 'false'.
Notes
Checking whether a file exists is a fundamental part of file operations. Because file_exists() returns true for both files and directories, use is_file() when you want to target regular files only.
PHP caches the stat results of file information, so calling a check function immediately after creating or deleting a file may return a stale result. Call clearstatcache() to clear the cache. Pay particular attention when creating, deleting, and checking the same file within a single request.
Always keep security in mind when working with files. If user input is included in a file path, validate the path using basename() or realpath() to prevent directory traversal attacks. Use mkdir() / unlink() to create or delete files.
If you find any errors or copyright issues, please contact us.