Open In App

PHP | is_readable( ) Function

Improve
Improve
Like Article
Like
Save
Share
Report

The is_readable() function in PHP used to check whether the specified file exists and is readable or not. The name of the file is sent as a parameter to the is_readable() function and it returns True if the file exists and is readable.
is_readable() function returns False for streams, for example, php://stdin.
is_readable() function can also be used with some URL wrappers such as file: // ,http:// ,ftp:// ,php:// in PHP 5.0.0.

Syntax:

is_readable($file)

Parameters Used:
The is_readable() function in PHP accepts only one parameter.

  • file : It is a mandatory parameter which specifies the file.

Return Value:
It returns True if the file or directory specified exists and is readable otherwise it returns False.

Exceptions:

  1. An E_WARNING is emitted on failure.
  2. The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.

Below programs illustrate the is_readable() function.

Program 1




<?php 
$myfile = "gfg.txt";
  
// checking whether file is readable or not
if (is_readable($myfile)) 
{
    echo '$myfile is readable';
else 
{
    echo '$myfile is not readable';
}
?>


Output:

gfg.txt is readable

Program 2




<?php 
$myfile = "gfg.txt";
  
// checking whether file is readable or not
if (is_readable($myfile)) 
{
    echo '$myfile is readable';
  
    // displaying contents of the uploaded file
    echo "Contents of the file are :\n";
    readfile($myfile);
else 
{
    echo '$myfile is not readable';
}
?>


Output:

gfg.txt is readable
Contents of the file are :
Portal for geeks!

Program 3




<?php 
  
$permissions = fileperms("gfg.txt");
$permvalue = sprintf("%o", $permissions);
  
// Clearing the File Status Cache 
clearstatcache();
  
if(is_readable("gfg.txt")) 
   echo("File is Readable 
    and File Permissions are : $permvalue)");
else
{
  echo("File is Not Readable 
     and File Permissions are : $permvalue)");
  
// Clearing the File Status Cache
clearstatcache();
?>


Output:

File is Readable and File Permissions are : 0644

Related Article: PHP | is_writable() Function

Reference:
http://php.net/manual/en/function.is-readable.php



Last Updated : 06 Jun, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads