PHP is_file() Function

What is PHP is_file() Function?

To check whether a path is a regular file or not, use is_file() function. In other words, this function also declares the existence of the file.

Syntax:

is_file(path)

Parameters:

The Function has 1 parameter which is required-

path (Required): it is the path to the file that you want to check whether it is file or not. The function checks the path relative to the current directory. If you mention a hard link or symbolic link as path, then the function resolves and check it.

Return Values:

The function returns-

  • TRUE – if the path mentioned as parameter is a file. Check example 1.
  • FALSE – if the path in the parameter is not a file or If the parameter is empty or If the specified file doesn’t exist (Check example 1).

How to use is_file() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result that the conditional statement returns, your program flow progresses. Look at the code below-

<?php
if (is_file()("images/")) {
    echo 'This is a directory.';
} else {
    echo ' This is not a directory.';
}
?>

Examples:

Example 1:

<?php // #1
var_dump(is_file("file/demy.txt"));
echo "<br />";
var_dump(is_file("fakefoleder.txt"));
echo "<br />";
var_dump(is_file(""));
echo "<br />";
var_dump(is_file("file/"));
?>

Output:

bool(true)
bool(false)
bool(false)
bool(false)

Explanation:

Line 2: the file “demy.txt” exists in the file directory which is in the current directory, so, the function returns TRUE.

Line 4: the file “fakefolder.txt” doesn’t exist in the current directory, so, the function returns FALSE.

Line 6: the function has no parameter, so it indicates no file and the function returns FALSE.

Line 8: “file/” is a directory which exists in the current directory, so, it is not a file and hence the function returns FALSE.

Notes on is_file() Function:

The function doesn’t consider a web address as a directory. So, it returns “php.net is not a file” in the following example-

<?php // #2
$path = "php.net";
if (is_file($path)){
    echo ("$path is a file"); 
}else{
    echo ("$path is not a file");
}
?>

Caution:

PHP cache the result of the is_file() function. So if you call this function multiple times in a script PHP sends you the cached result. The problem is if existence of the directory, you won’t get the up-to-date result. To get the latest result, clear the cache with clearstatcache() function. Check the code below-

<?php
$path = "css/";
var_dump(is_file($path));
// Some codes.
var_dump(is_file($path));
// Some codes.
clearstatcache();
var_dump(is_file($path));
// Some codes.
?>

Difference Between is_file() and file_exists() Functions:

When both is_file() and file_exists() functions return TRUE, it means that the file exists. Also, it is a file if is_file() function returns it, but, it could be either a file or a function when file_exists() function returns it. See the example below. The folder “file” is in the current directory and the file “demo.txt” exists in this folder.

<?php
var_dump(is_file("file/demo.txt"));
echo "<br />";
var_dump(file_exists("file/demo.txt"));
echo "<br />";
var_dump(file_exists("file/"));
?>

Output:

bool(true)
bool(true)
bool(true)

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_file() Function

When you develop a web app that deal with file, you check whether a path is a file or not before opening it. This function helps you to test it. it is one of the built-in filesystems function in PHP.

Reference:

https://www.php.net/manual/en/function.is-file.php