Computer >> Computer tutorials >  >> Programming >> PHP

is_readable() function in PHP


The is_readable() function checks whether a file is readable. The function returns TRUE if the file or directory exists and is readable. It returns FALSE if the file or directory does not exist.

Syntax

is_readable(file_path)

Parameters

  • file_path − Specify the file to check.

Return

The is_readable() function returns TRUE if the file or directory exists and is readable. It returns FALSE if the file or directory does not exist.

Example

<?php
$file_path = "new.txt";
if(is_readable($file_path)) {
   echo ("Readable!");
} else {
   echo ("Not readable!");
}
?>

Output

Not readable!

Let us see another example that also reads the file if it is readable.

We have a file “demo.txt” with the following content.

This is demo text in demo file!

The following is the code that checks whether the file is readable or not. If it is readable, then the file content is also displayed.

Example

<?php
$file_path = "demo.txt";
if(is_readable($file_path)) {
   echo ("Readable!");
   echo ("Reading file: ");
   readfile($file_path);
} else {
   echo ("Not readable!");
}
?>

Output

Not readable!