PHP is_readable() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, is_readable() tells you whether a file or folder exists and PHP is allowed to read it. It returns true or false.

Use it before you open or read a file so that your script can show a friendly message instead of a warning. It checks the permissions of the user that runs PHP, such as the web server user.

Syntax

Call is_readable() with the values you need. Start with this line.

PHP
$canRead = is_readable('notes.txt');
CallWhat it does
is_readable('notes.txt')Returns true if notes.txt exists and can be read.
is_readable(__DIR__)Returns true for a folder that can be listed.
is_readable('missing.txt')Returns false because the file does not exist.

A true result means that PHP can read the path right now. The file can still change before you open it, so you should also check the return value of fopen() or file_get_contents().

Library declarations
PHP
is_readable(string $filename): bool

Parameters

ParameterDescription
filenameRequired. The path to the file or directory to check.

Return Value

Return ValueDescription
boolReturns true if the path exists and is readable, otherwise false.

PHP Examples

Example 1 — Check Before Reading a File

Create a file and read it only when is_readable() says it is safe.

PHP
<?php
$file = sys_get_temp_dir() . '/is_readable_demo.txt';
file_put_contents($file, 'Hello CodeToFun');

if (is_readable($file)) {
    echo file_get_contents($file), PHP_EOL;
}

unlink($file);

How It Works

  1. file_put_contents() creates a file that PHP itself can read.
  2. is_readable() returns true, so the if block runs.
  3. file_get_contents() reads the content and echo prints it.
  4. unlink() removes the temporary file.

Example 2 — Missing Files and Folders

A missing path is not readable. A folder, on the other hand, is readable, which can surprise beginners.

PHP
<?php
$dir = sys_get_temp_dir();

echo "Missing file: ", is_readable($dir . '/no-such-file.txt') ? 'yes' : 'no', PHP_EOL;
echo "Folder: ", is_readable($dir) ? 'yes' : 'no', PHP_EOL;
echo "Folder is a file: ", is_file($dir) ? 'yes' : 'no', PHP_EOL;

How It Works

  1. A file that does not exist cannot be read, so the first line prints no.
  2. The temporary folder exists and PHP may list it, so is_readable() returns true.
  3. is_file() shows that the same path is not a regular file.
  4. Combine is_file() and is_readable() when you want a readable file only.

Example 3 — A Safe File Reader Function

Wrap the checks in a small function that returns the text or a clear message.

PHP
<?php
function readSafe(string $path): string
{
    if (!is_file($path) || !is_readable($path)) {
        return "File not readable";
    }
    return file_get_contents($path);
}

$file = sys_get_temp_dir() . '/is_readable_safe.txt';
file_put_contents($file, 'Hello');

echo readSafe($file), PHP_EOL;
echo readSafe($file . '.missing'), PHP_EOL;

unlink($file);

How It Works

  1. readSafe() first checks that the path is a regular file and can be read.
  2. If either check fails it returns a message instead of letting PHP raise a warning.
  3. The real file passes both checks, so its content is returned.
  4. The missing path fails the first check and returns the message.

Frequently Asked Questions

It checks whether a file or folder exists and can be read by the user that runs PHP.
Yes. It returns true for a folder that PHP may list. Add is_file() if you need a regular file.
PHP runs as a different user, such as the web server user, who may not have permission. Check the file permissions and the owner.
It is a good first check, but the file can change after the check. Still test the result of fopen() or file_get_contents().
file_exists() only says that the path exists. is_readable() also checks that PHP has the right to read it.

Next: is_uploaded_file()

is_uploaded_file() tells you whether a file was really uploaded through an HTTP POST form.

is_uploaded_file() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful