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:
- The file_exists() function returns False if the path specified points to non-existent files.
- 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.