PHP is_readable: Check File Accessibility

php is_readable function

The PHP is_readable helps you validate file readability before any operation like read or include files.

What Is PHP is_readable?

The is_readable() function checks if a given file or directory exists and whether it can be read. It is a built-in PHP function that returns a boolean value—true if the file is readable and false otherwise.

Here is its syntax:

is_readable($filename)
  • Parameter: $filename is the file or directory path you want to check.
  • Return Value: The function returns true if the specified file or directory exists and is readable; otherwise, it returns false.

Here is a quick example:

$file = 'example.txt';

if (is_readable($file)) {
    echo "File is readable.";
} else {
    echo "File cannot be read.";
}

The example above checks if example.txt can be read with is_readable. This helps make sure your program won’t crash with unexpected errors.

The Difference Between is_readable and is_file

You may like to check about the below:

  • Is this a real file?
  • Can I open this file and read it?

PHP gives you different functions to check this. Two common ones are is_file() and is_readable(). Let’s look at what they mean.

The is_file function checks if something is a file. It returns true if the path points to a real file (not a folder).

But, is_readable checks if the file or folder can be read. It returns true if your PHP script has permission to open and read it.

Here is a table that shows you the key differences:

Featureis_file()is_readable()
PurposeChecks if it is a fileChecks if it can be read
Works on folders?NoYes
Checks permissions?NoYes
Example UseIs this path a file?Can I open and read this file?

You can use both functions together to make sure:

  • The file exists
  • It’s really a file
  • It can be read

Here is an example:

if (is_file("example.txt") && is_readable("example.txt")) {
    echo "This is a readable file.";
}

Examples of PHP is_readable

Validate a File Before read it:

$configFile = 'config.json';

if (is_readable($configFile)) {
    $content = file_get_contents($configFile);
    echo "Configuration loaded successfully.";
} else {
    echo "Cannot access the configuration file.";
}

This makes sure your script doesn’t crash if the file is inaccessible or doesn’t exist.

Ensure Dynamic Includes Work:

$fileToInclude = 'plugin.php';

if (is_readable($fileToInclude)) {
    include $fileToInclude;
} else {
    echo "Plugin file not found.";
}

When your application dynamically includes files, is_readable verifies the file’s existence and readability.

Handle Directory Readability:

$directory = 'uploads';

if (is_readable($directory)) {
    echo "Directory is accessible.";
} else {
    echo "Directory cannot be read.";
}

You have to know that the function is not limited to files—it also works with directories.

Wrapping It Up

In this article, you learned what the is_readable() function does and how it helps you check if files or directories can be read in PHP. You also understood the difference between is_readable() and is_file().

Here is a quick recap:

  • is_readable() checks if a file or folder can be opened and read by your script.
  • It returns true if the path exists and is readable and false if it isn’t.
  • is_file() checks if the path is a real file (not a folder).
  • Use both functions together to be extra safe when you write a code to read or include PHP files.
  • is_readable() works with both files and directories.
  • You saw examples like how to check for config files. That includes PHP plugins and directories that are readable.

FAQs

What does `php is_readable` do?

The is_readable() checks if a file or directory exists and PHP can read it. It returns true when both conditions are met, otherwise it returns false and logs a warning.

Does `is_readable` clear its results cache automatically?

No. PHP caches file status. If you change permissions or file location, call clearstatcache() before is_readable() to get fresh results

Does `is_readable` work on URLs or streams?

No. It doesn't work reliably with URLs or non‑filesystem streams like php://stdin. Stick to local files or directories

Should I use `file_exists()` before `is_readable()`?

No. is_readable() already checks existence. Use is_readable() alone. For files only, combine it with is_file().

How does `is_readable` differ from `is_writable`?

  • is_readable() checks read access.
  • is_writable() checks write access.
They operate independently. Use the one matching your need

Similar Reads

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

PHP Switch | How the Switch Statement Works in PHP

The PHP switch statement is multiple conditions to only execute one block if its condition generating a true boolean value.…

PHP Spread Operator: Understand How (…) Syntax Works

Before the spread operator, PHP developers had to pass array items to a function using extra steps. They often had…

PHP Assign Function to Variable – Examples and Explanation

PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…

How to Select Data Using PHP and MySQL?

In this article, You will understand everything you need to know about how to use PHP to select data from…

PHP array_intersect_assoc: How it Works with Examples

PHP array_intersect_assoc compares values and keys in arrays. It returns matches with the same key and value from all arrays…

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

PHP break Statement: How to Exit a Loop

The PHP break statement helps in controlling the flow of loop work. It gives you a way to stop a…

PHP is_dir Function: Check if a Directory Exists

Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

Previous Article

PHP file_exists: Check File and Directory Existence

Next Article

PHP is_file Function: Check if a File Exists

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.