PHP | is_file( ) Function

The is_file() function in PHP is an inbuilt function which is used to check whether the specified file is a regular file or not. The name of the file is sent as a parameter to the is_file() function and it returns True if the file is a regular file else it returns False.

Syntax:

bool is_file($file)

Parameters Used:
The is_file() function in PHP accepts one parameter.



  • $file: It is a mandatory parameter which specifies the file.

Return Value:
It returns True if the file is a regular file else it returns false.

Exceptions:

  • An E_WARNING is emitted on failure.
  • The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.
  • is_file() function returns false for non-existent files.
  • is_file() function may return unexpected results for files which are larger than 2GB since PHP’s integer type is signed and many platforms use 32bit integers.

Below programs illustrate the is_file() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$myfile = "gfg.txt";
  
// checking whether the file is a 
// regular file or not
if (is_file($myfile)) {
    echo ("$myfile: regular file!");
} else {
    echo ("$myfile: not a regular file!");
}
?>

chevron_right


Output:

gfg.txt: regular file!

Program 2

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$myfile = "gfg.txt";
  
// checking whether the file is a 
// regular file or not
if (is_file($myfile)) {
    echo ("$myfile: regular file!");
      
    // display the content of regular file
    echo "Contents of the file are :\n";
    readfile($myfile);
} else {
    echo ("$myfile: not a regular file!");
}
?>

chevron_right


Output:

gfg.txt: regular file!
Contents of the file are :
Portal for geeks!

Reference:
http://php.net/manual/en/function.is-file.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.