PHP is_readable() Function

What is PHP is_readable() Function?

If you want to know whether a file or directory exist or not and is readable, use PHP is_readable() function. In your web app development, you want to check whether file is readable before you open it. In this case you can use this function.

Syntax:

is_readable(filename)

Parameters:

The Function has 1 parameter which is required-

filename (Required): It is the path to the file or directory whose readability you want to check.

Return Values:

The function returns-

  • TRUE – if the parameter is a file or directory and is readable.
  • FALSE – if the parameter doesn’t exist and hence not readable or it exists but not readable.

How to use is_readable() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result that the conditional statement returns, your program flow progresses. Look at the code below-

<?php
if (is_readable("file/demo.txt")) {
    echo 'The file is readable';
} else {
    echo 'The file is not readable';
}
?>

Examples:

Example 1: Confirming that the file exists but not readable-

<?php
if(file_exists("file/deny.txt")){
    if (is_readable("file/demo.txt")) {
        echo 'The file is readable';
    } else {
        echo 'The file exists but is not readable.';
    }
}else{
    echo "File doesn't exist.";
}
?>

Output:

The file exists but is not readable.

Explanation:

Here, file_exists() function confirms that that file “deny.txt” exists. And the is_readable() function confirms that the file exists and is not readable.

Example 2: Checking whether an entry is a file and is readable-

<?php
$filename = "file/demo.txt";
if (is_file($filename) && is_readable($filename)) {
    echo "It is a file and it is readable.";
}else{
    echo "It is not a file.";
}
?>

Output:

It is a file and it is readable.

Explanation:

The is_file() function confirms that “demo.txt” is a file and the is_readable() function confirms that the file is readable. Otherwise the output would be “It is not a file.”

Example 3: Checking whether an entry is a directory and is readable-

<?php
$filename = "file/";
if (is_dir($filename) && is_readable($filename)) {
    echo "It is a directory.";
}else{
    echo "It is not a directory.";
}
?>

Output:

It is a directory and it is readable.

Explanation:

The is_dir() function confirms that “file/” is a directory and the is_readable() function confirms that the directory is readable. Otherwise, the output would be “It is not a directory.”

Notes on is_readable() Function:

  • To check whether the file exists or not, you don’t need to use file_exists() with is_readable() function. Because, when is_readable() function returns TRUE, it confirms the file’s existence.
  • But, when the function returns FALSE, it may happen that the file doesn’t exists or the file exists but not readable. So, confirming file’s existence with file_exists() function, if you use is_readable() function, and it returns FALSE, it means that the file is not readable. Check example 1.
  • As the function returns TRUE for both file and directory, you can use is_file() function with this function to ensure that it is a file. Check example 2.
  • Similarly, as the function returns TRUE for both file and directory, you can use is_dir() function with this function to ensure that it is a directory. Check example 3.

Caution:

  • PHP cache the result of the is_readable() function. So if you call this function multiple times in a script PHP sends you cached result. The problem is if you modify the file, you won’t get the up-to-date result of the file. So, to get the latest result, clear the cache with clearstatcache() function. Check the code below-
    <?php
    $filename = "file/deny.txt";
    var_dump(is_readable($filename));
    // Some codes.
    var_dump(is_readable($filename));
    // Some codes.
    clearstatcache();
    var_dump(is_readable($filename));
    // Some codes.
    ?>
    

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_readable() Function

is_readable() which is one of the built-in PHP filesystem functions is very useful when you work with files and directories. It confirms you whether an entry is file/directory and then you can do the other related tasks.

Reference:

https://www.php.net/manual/en/function.is-readable.php