PHP file_exists( ) Function Last Updated : 15 May, 2025 Comments Improve Suggest changes Like Article Like Report The file_exists() function in PHP checks whether a file or directory exists on the server. It returns a boolean value:true: If the file or directory exists.false: If the file or directory does not exist or the path is incorrect.Syntax:file_exists($path)In this syntax: 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:true: If the file or directory exists.false: If the file or directory does not exist, or the path is incorrect.Example Usage of file_exists()Let's take a simple example that checks if a file exists using file_exists(). PHP $file = "example.txt"; if (file_exists($file)) { echo "The file '$file' exists."; } else { echo "The file '$file' does not exist."; } ?> In this example:we specify the file name as "example.txt".If the file exists in the current directory, the message The file 'example.txt' exists. will be displayed.If the file doesn't exist, the message The file 'example.txt' does not exist. will be shown.Checking a DirectoryThe file_exists() function also works with directories. Here's an example that checks if a directory exists: PHP $directory = "uploads/"; if (file_exists($directory)) { echo "The directory '$directory' exists."; } else { echo "The directory '$directory' does not exist."; } ?> In this example:In this example, we check if a directory named "uploads/" exists.If the directory exists, the browser will display: The directory 'uploads/' exists.ConclusionThe file_exists() function in PHP is a powerful tool for verifying the existence of files or directories before performing operations on them. It helps prevent errors by ensuring that files are available for reading, writing, or manipulation, and can be used in conjunction with other functions like is_file() and is_dir() to get more detailed information about the file system. Understanding how to properly use file_exists() will make your file-handling scripts more robust and error-free. Comment More infoAdvertise with us Next Article PHP file_exists( ) Function S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-file-handling Practice Tags : Misc Similar Reads PHP basename( ) Function The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: string basename ( $path , $suffix ) Parameters: The basename() function in PHP accepts two parameters which are 2 min read PHP chgrp( ) Function The chgrp() function in PHP is an inbuilt function that is used to change the user group of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the group of a file arbitrarily. Syntax: bool chgrp ( $filename, $group ) Parameters: The chgrp( 2 min read PHP chmod( ) Function The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user. The chmod() function changes the permissions of the specified file and returns true on success and false on failure. Syntax: bool chmod ( string $filename, i 2 min read PHP chown( ) Function The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file. Syntax: bool chown ( $filename, $user ) Parameters: The chown() function in PHP 2 min read PHP copy( ) Function The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bo 2 min read PHP dirname( ) Function The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.' 2 min read PHP disk_free_space( ) Function The disk_free_space() function in PHP is an inbuilt function which is used to return the amount of free space in a specified directory. The disk_free_space() function denotes the free space in bytes. It returns the available space on a filesystem or on a disk partition. The disk_free_space() functio 2 min read PHP disk_total_space( ) Function The disk_total_space() function in PHP is an inbuilt function which is used to return the total space of a specified directory. The disk_total_space() function denotes the total space in bytes. It returns the total space on a filesystem or on a disk partition. The disk_total_space() function returns 2 min read PHP fclose() Function The fclose() function in PHP closes a file that was previously opened by fopen(). Closing a file releases the resource associated with it and makes sure all the data written to the file is properly saved. Not closing files can lead to resource leaks or incomplete data writing.Syntax:bool fclose(reso 2 min read PHP feof( ) Function The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the "end-of-file" has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand. The feof() func 2 min read Like