How can the use of file_exists() function help in handling file inclusions securely in PHP?

When including files in PHP, it's important to ensure that the file being included actually exists to prevent potential security vulnerabilities like remote file inclusion attacks. The file_exists() function can be used to check if a file exists before including it, thus helping to handle file inclusions securely.

$file = 'path/to/file.php';

if (file_exists($file)) {
    include $file;
} else {
    // Handle the case when the file does not exist
    echo "File does not exist.";
}