Friday, September 5, 2025
HomeLanguagesPHP | file_exists( ) Function

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:




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


Output:

1

Program 2:




<?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";
}
  
?>


Output:

1

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

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

RELATED ARTICLES

Most Popular

Dominic
32265 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11864 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6719 POSTS0 COMMENTS