PHP Function Reference

PHP is_file() Function



The PHP is_file() function checks whether the given filename is a regular file.

Note: The results of this function are cached. Use clearstatcache() function to clear the cache.

Syntax

is_file(filename)

Parameters

filename Required. Specify the path to the file to check.

Return Value

Returns true if the filename exists and is a regular file, otherwise returns false.

Exceptions

Upon failure, an E_WARNING is thrown.

Example:

Lets assume that we have a file called test.txt in the current working directory. The example below demonstrates on using this function to check whether the given filename is a regular file.

<?php
$dir1 = 'test.txt';
$dir2 = 'temp/test';
$dir3 = 'demo.txt';

//checking if $dir1 exists and is a regular file
if(is_file($dir1)) {
  echo "$dir1 is a regular file.\n";
} else {
  echo "$dir1 is not a regular file.\n";
}

//checking if $dir2 exists and is a regular file
if(is_file($dir2)) {
  echo "$dir2 is a regular file.\n";
} else {
  echo "$dir2 is not a regular file.\n";
}

//checking if $dir3 exists and is a regular file
if(is_file($dir3)) {
  echo "$dir3 is a regular file.\n";
} else {
  echo "$dir3 is not a regular file.\n";
}
?>

The output of the above code will be:

test.txt is a regular file.
temp/test is not a regular file.
demo.txt is not a regular file.

❮ PHP Filesystem Reference