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

PHP Filesystem is_file() Function



The PHP Filesystem is_file() function is used to check that the specified path is a file. This method helps you confirm that a given path refers to a file and not a directory/folder or other type of resource.

Syntax

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

bool is_file ( string $filename )

Parameters

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

Sr.No Parameter & Description
1

$filename(Required)

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

Return Value

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

PHP Version

The is_file() 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_file() function to check that the given path refers to a file or not.

<?php
   // Mention the directory path here
   $path = '/Desktop/PhpProjects/myfile.txt';

   if (is_file($path)) {
      echo "The path is a file.";
   } else {
      echo "The path is not a file.";
   }
?>

Output

Here is the outcome of the following code −

The path is a file.

Example

This PHP example will perform the operation to check if a path is a file or a directory with the help of is_file() function.

<?php
   //mention your path here
   $path = '/var/www/html';

   // Check if the given path is file or directory
   if (is_file($path)) {
      echo "The path is a file.";
   } else {
      echo "The path is not a file.";
   }
?> 

Output

This will produce the following result −

The path is not a file.

Example

In this PHP code, we will use is_file() function with a relative path. So if the mentioned path is a relative path so it will return true otherwise false.

<?php
   // Mention the relative path here
   $relativePath = './uploads/myfile.txt';

   if (is_file($relativePath)) {
       echo "The relative path is a file.";
   } else {
       echo "The relative path is not a file.";
   }
?> 

Output

This will generate the below result −

The relative path is a file.

Example

In this PHP code, we will use is_file() function to check multiple paths given in the array $paths. So it will check if the given paths are files or not.

<?php
   // Mention multiple paths here
   $paths = ['/var/www/html/index.php', '/var/www/html/uploads/contacts.php', '/var/www/html'];

   foreach ($paths as $path) {
      if (is_file($path)) {
         echo "The path '$path' is a file.\n";
      } else {
         echo "The path '$path' is not a file.\n";
      }
   }

?> 

Output

This will produce the following output −

The path '/var/www/html/index.php' is a file.
The path '/var/www/html/uploads/contacts.php' is a file.
The path '/var/www/html' is not a file.

Note

As PHP's integer type is signed and many platforms use 32-bit integers, multiple Filesystem methods can generate unexpected results for files larger than 2GB.

Summary

The is_file() function is a powerful function for checking the given path is a file in PHP. This method is useful to verify that files exist before doing file operations.

php_function_reference.htm
Advertisements