PHP | is_readable( ) Function (original) (raw)

Last Updated : 06 Jun, 2018

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.

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" ;

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" ;

if ( is_readable ( $myfile ))

{

`` echo '$myfile is readable' ;

`` 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 );

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 )");

}

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