file_exists() is a built-in PHP function used to determine whether a specified file or directory exists. Its basic usage is as follows:
$path = '/path/to/file.txt';
<p>if (file_exists($path)) {<br>
echo 'File exists';<br>
} else {<br>
echo 'File does not exist';<br>
}<br>
This function returns a boolean value: it returns true if the file or directory exists, and false otherwise.
is_readable() is also a built-in function, used to check if the specified path is readable. This is especially important for file reading operations. For example:
$path = '/path/to/file.txt';
<p>if (is_readable($path)) {<br>
echo 'File is readable';<br>
} else {<br>
echo 'File is not readable';<br>
}<br>
This function also returns a boolean: it returns true if the file or directory can be read.
Using file_exists() alone does not fully guarantee that subsequent read operations will succeed. For example, a file may exist, but if the current user lacks permission to read it, using file_get_contents() directly may trigger warnings or even halt the program. Therefore, the best practice is to combine the two functions:
$path = '/var/www/gitbox.net/uploads/data.json';
<p>if (file_exists($path) && is_readable($path)) {<br>
$content = file_get_contents($path);<br>
echo 'File content is as follows:<br>';<br>
echo nl2br(htmlspecialchars($content));<br>
} else {<br>
echo 'File does not exist or is not readable';<br>
}<br>
In this example, the program first checks if the file exists, then whether it is readable, and only proceeds to read the file with file_get_contents() if both conditions are met.
For instance, we have a configuration file config.json stored in the project’s root directory. During project initialization, you may need to read configuration data from this file:
$configPath = __DIR__ . '/config/config.json';
<p>if (file_exists($configPath) && is_readable($configPath)) {<br>
$config = json_decode(file_get_contents($configPath), true);<br>
// Continue processing the configuration<br>
} else {<br>
die('Configuration file is missing or not readable');<br>
}<br>
This approach helps prevent many issues caused by missing files or insufficient permissions, improving the robustness of your program.
If you want to check whether a remote file (such as https://gitbox.net/resources/info.txt) exists and is accessible, file_exists() and is_readable() are not suitable as they only work with the local file system. To check remote files, you usually need to use methods like curl or get_headers():
$url = 'https://gitbox.net/resources/info.txt';
$headers = @get_headers($url);
if ($headers && strpos($headers[0], '200') !== false) {
echo 'Remote file exists';
} else {
echo 'Remote file does not exist or is inaccessible';
}
In everyday development, if you need to read a file, always confirm its “availability”—this means checking both whether the file exists and whether you have permission to read it. file_exists() and is_readable() are two core functions for achieving this. Using them together wisely makes your code more robust and secure.