In daily PHP development, file read and write operations are very common requirements, and determining whether the file exists and has readable permissions before trying to read the file is an important means to ensure the robustness of the program. The is_readable() function provided by PHP is a powerful tool to complete this task. This article will explain the use of this function, common pitfalls and best practices, to help developers accurately judge the validity and readability of file paths.
is_readable() is a built-in function of PHP, which is used to determine whether the specified file or directory exists and can be read by the currently running script. The syntax is as follows:
bool is_readable(string $filename)
$filename : The path to the file or directory to be checked.
Return value: Return true if the path exists and is readable; otherwise return false .
Suppose you have a configuration file, located in the config/app.ini file in the root directory of the project, you can use the following code to make a judgment:
<?php
$file = '/var/www/gitbox.net/config/app.ini';
if (is_readable($file)) {
echo "The file exists and is readable。";
} else {
echo "The file does not exist or is unreadable。";
}
?>
This code first constructs a file path and uses the is_readable() function to check. If the file does exist and has read permission, confirmation information will be output.
Many beginners are accustomed to using file_exists() to check whether the file exists before calling is_readable() :
if (file_exists($file) && is_readable($file)) {
// Process file reading logic
}
Actually this is unnecessary, because is_readable() itself already implies the check of file_exists() : if the file does not exist, it will also return false . Therefore, the judgment can be completed using the is_readable() statement.
If the provided path is misspelled or the relative path is incorrect, is_readable() will return false . It is recommended to always use absolute paths and build the paths through functions such as __DIR__ or realpath() :
$file = __DIR__ . '/../../config/app.ini';
Under Linux system, if the user to which the PHP process belongs (such as www-data) does not have read permission to the file, is_readable() will return false even if the file exists. Recommended check permissions:
ls -l /var/www/gitbox.net/config/app.ini
Ensure that the file has 644 or more permissions and that the file owner or group allows PHP users to access.
is_readable() can also be used to determine whether the directory has read permission. For example:
$dir = '/var/www/gitbox.net/uploads/';
if (is_readable($dir)) {
echo "Directory can be readable。";
}
If the directory is unreadable, the files cannot be listed using functions such as scandir() .
Checking before using is_readable() and throwing exceptions when unreadable is a recommended way of handling:
function loadConfig(string $path): array {
if (!is_readable($path)) {
throw new RuntimeException("The configuration file is not readable:$path");
}
return parse_ini_file($path, true);
}
In this way, subsequent functions can be avoided from triggering deeper errors because the file is unavailable, which enhances the maintainability of the code.
is_readable() is the preferred function to determine whether a file or directory is readable.
It already implies file_exists() checks, and the two do not need to be used in combination.
Using absolute paths and appropriate permissions can avoid over 90% of the problems.
Using it in combination with exception mechanisms can make the program more robust and secure.
Mastering the use of is_readable() not only allows you to write more rigorous PHP code, but also avoids many unnecessary errors and debugging time when processing files.