PHP | file_exists( ) Function

The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not.

The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.

Syntax:



file_exists($path)

Parameters: The file_exists() function in PHP accepts only one parameter $path. It specifies the path of the file or directory you want to check.

Return Value: It returns True on success and False on failure.

Errors And Exception:

  1. The file_exists() function returns False if the path specified points to non-existent files.
  2. For files larger than 2gb, some of the filesystem functions may give unexpected results since PHP’s integer type is signed and many platforms use 32bit integers.

Examples:

Input : echo file_exists('/user01/work/gfg.txt');
Output : 1

Input : $file_pointer = '/user01/work/gfg.txt';
        if (file_exists($file_pointer)) {
            echo "The file $file_pointer exists";
        }else {
            echo "The file $file_pointer does 
                                   not exists";
        }
Output : 1

Below programs illustrate the file_exists() function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// checking whether file exists or not
echo file_exists('/user01/work/gfg.txt');
  
?>

chevron_right


Output:

1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// checking whether file exists or not
$file_pointer = '/user01/work/gfg.txt';
  
if (file_exists($file_pointer)) 
{
    echo "The file $file_pointer exists";
}
else 
{
    echo "The file $file_pointer does
                             not exists";
}
  
?>

chevron_right


Output:

1

Reference:
http://php.net/manual/en/function.file-exists.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.