What is the purpose of the file_exists() function in PHP and how is it commonly used?

The file_exists() function in PHP is used to check if a file or directory exists on the server. It returns true if the file exists and false if it does not. This function is commonly used to verify the existence of a file before performing operations such as reading, writing, or deleting it.

$file_path = "path/to/file.txt";

if (file_exists($file_path)) {
    echo "File exists!";
} else {
    echo "File does not exist!";
}