is_readable
is_readable($filename)
Returns true when the file or folder exists and PHP has permission to read it. Otherwise it returns false.
Parameters
- $filename – a path to a file or folder.
Examples
$file = sys_get_temp_dir() . "/garth-example.txt"; file_put_contents($file, "Hello"); var_dump(is_readable($file)); // bool(true) // a missing file var_dump(is_readable(sys_get_temp_dir() . "/missing.txt")); // bool(false) // loading with a fallback value $text = is_readable($file) ? file_get_contents($file) : ""; echo $text; // Hello unlink($file);
Things to watch out for
- The permissions are judged for the user PHP runs as (on a server usually the web server user), not for your FTP account.
- True does not mean that reading will definitely succeed. The file can disappear in the meantime, or another program can lock it. So check the result of reading anyway.
- On Windows PHP practically does not distinguish read permissions, the differences only show up on a Linux server.
- PHP remembers the result during the script. After another program changes the permissions, call clearstatcache().
Related functions:
is_writable()
file_exists()
file_get_contents()
is_executable()