What is the is_file() function in PHP?

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

Syntax

is_file(string $name): bool

Parameter

This function takes a single parameter.

name: This specifies the path to the file we want to check.

Return value

The is_file() function returns true if the provided filename exists and is also a regular file. Otherwise, it returns false.

Code

The code below shows how to use the is_file function.

main.php
edpresso.txt
<?php
$name = "website.txt";
if(is_file($name))
{
echo ("$name is a regular file");
}
else
{
echo ("$name is not a regular file");
}
echo "\n";
$name2 = "edpresso.txt";
if(is_file($name2))
{
echo ("$name2 is a regular file");
}
else
{
echo ("$name2 is not a regular file");
}
?>

Explanation

In the above code, two filenames have been specified, website.txt and edpresso.txt. The former filename does not exist, so the is_file function returns false.

On the other hand, the function returns true when called with expresso.txt since it is a valid file.

This function produces cache output. We have to clear the cache by using the clearstatcache() function.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved