PHP | is_readable( ) Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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';
}
?>

chevron_right


Output:

gfg.txt is readable

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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';
}
?>

chevron_right


Output:

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

Program 3

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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();
?>

chevron_right


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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.