What are the potential pitfalls of using file_exists() function in PHP for including files?

Using file_exists() function for including files can lead to security vulnerabilities such as path traversal attacks. It is recommended to use more secure methods such as realpath() to sanitize the file path before including it.

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

if (file_exists(realpath($file))) {
    include(realpath($file));
} else {
    echo 'File does not exist';
}