is_readable()
The is_readable() function is a built-in PHP function that checks whether a file is readable. This function returns true if the file is readable, and false
The is_readable() function tells you whether PHP can read a given file or directory before you try to open it. It checks that the path exists and that the current process has read permission on it. Calling it first lets you fail gracefully instead of triggering a runtime warning from fopen() or file_get_contents().
This chapter covers the syntax, the return value, a runnable example, the most common gotchas (caching, permissions, race conditions), and how is_readable() relates to the other PHP filesystem functions.
Syntax
is_readable(string $filename): bool| Part | Meaning |
|---|---|
$filename | Path to the file or directory to check. Can be absolute (/var/www/data.txt) or relative to the script's working directory. |
| Returns | true if the path exists and is readable by the current user/process, false otherwise. |
is_readable() works on directories too: it returns true when the directory can be opened and listed.
Basic example
The function returns a boolean, so it reads naturally inside an if condition: you branch on whether the file can be read without ever opening it.
A self-contained, runnable example
The example above points at a path that may not exist. Here is a version you can run anywhere — it creates a file, checks it, then checks a path that does not exist:
<?php
// Create a temp file we know is readable.
$file = tempnam(sys_get_temp_dir(), 'demo');
file_put_contents($file, 'hello');
var_dump(is_readable($file)); // bool(true)
var_dump(is_readable('/no/such/file')); // bool(false)
unlink($file); // clean upThe first call is true because the file exists and we just created it with our own permissions; the second is false because the path does not exist at all.
Guarding a file read
The typical real-world use is a guard before reading, so a missing or unreadable file does not emit a warning:
<?php
$path = 'config.json';
if (!is_readable($path)) {
// Handle the problem your way: log, default, or throw.
throw new RuntimeException("Cannot read config file: $path");
}
$config = json_decode(file_get_contents($path), true);Common gotchas
- Results are cached. PHP caches stat information per request. If you change a file's permissions during the same script run, call
clearstatcache()before checking again, or you may see a stale answer. trueis not a guarantee. Permissions can change between the check and the actual read (a time-of-check to time-of-use race). For critical code, just attempt the read and handle the error, rather than relying onis_readable()alone.- Returns
falsefor non-existent paths — it does not warn. Sois_readable()doubles as a "does this exist and can I read it" check. To test existence regardless of permissions, usefile_exists(). - Permissions are evaluated for the web server user (often
www-data), not your shell user, when the script runs under a web server.
Related functions
is_writable()— the write-permission counterpart.is_file()— checks that the path is a regular file (not a directory).file_exists()— checks existence, ignoring read permission.fopen()andfile_get_contents()— the functions you typically guard withis_readable().
Conclusion
is_readable() is a lightweight, side-effect-free way to check that a path exists and can be read before you open it. Use it as a guard to fail gracefully, remember that its results are cached within a request, and for security-critical code attempt the read and handle errors rather than trusting the check alone.