Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem is_readable() Function



The PHP Filesystem is_readable() function is used to check that the given file exists and it is readable of not. The function is helpful to make the code error-free by making sure that it only try to read files that are present and accessible.

Syntax

Below is the syntax of the PHP Filesystem is_readable() function −

bool is_readable ( string $filename )

Parameters

Below are the required parameters of the is_readable() function −

Sr.No Parameter & Description
1

$filename(Required)

It is the path to the file or directory that you want to check.

Return Value

The function is_readable() returns true if the path is a file or directory, or FALSE otherwise.

PHP Version

The is_readable() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7 and PHP 8.

Example

We have used the PHP Filesystem is_readable() function to check that the given path refers to a file or directory.

<?php
   // Mention the file path here
   $filename = '/PhpProjects/myfile.txt';
   if (is_readable($filename)) {
       echo 'The file is readable.';
   } else {
       echo 'The file is not readable.';
   }
?>

Output

Here is the outcome of the following code −

The file is readable.

Example

This PHP example will perform the operation to check that the given files are readable or not with the help of is_readable() function. And we will use array to store the path of multiple files.

<?php
   //mention your file path here
   $files = ['/PhpProjects/myfile.txt', '/PhpProjects/sample.txt', '/PhpProjects/newfile.txt'];

   foreach ($files as $file) {
       if (is_readable($file)) {
           echo "The file $file is readable.\n";
       } else {
           echo "The file $file is not readable.\n";
       }
   }
?> 

Output

This will produce the following result −

The file /PhpProjects/myfile.txt is readable.
The file /PhpProjects/sample.txt is readable.
The file /PhpProjects/newfile.txt is readable.

Example

In this PHP code, we will use is_readable() function to check that the given directory has the permission to read its content or not. So it will return true if it is readable otherwise false.

<?php
   // Mention the relative path here
   $directory = '/PhpProjects';

   if (is_readable($directory)) {
      echo "The directory is readable.";
   } else {
      echo "The directory is not readable.";
   }
?> 

Output

This will generate the below result −

The directory is readable.

Example

In this PHP code, we will use is_readable() function to check if a file is readable before trying to open it. So if it is readable then it will return true otherwise it will return false.

<?php
   // Mention multiple paths here
   $file = '/PhpProjects/myfile.txt';

   if (is_readable($file)) {
       $handle = fopen($file, 'r');
       if ($handle) {
           echo "The file is open for reading.";
           fclose($handle);
       }
   } else {
       echo "The file is not readable.";
   }
?> 

Output

This will produce the following output −

The file is open for reading.

Note

This function can return a true result for directories. In that case you can use is_dir() to differentiate between a file and a directory.

Summary

The is_readable() function is a powerful function for checking the given file or directory is readable or not. This method is useful to verify the file is accessible to read before opening it to save the unnecessary work.

php_function_reference.htm
Advertisements