How can the file_exists function be used to improve the file handling in PHP scripts?

The file_exists function can be used to check if a file exists before performing any file handling operations in PHP scripts. This can help prevent errors and improve the reliability of the script by ensuring that the file being accessed actually exists.

$filename = 'example.txt';

if (file_exists($filename)) {
    // File exists, perform file handling operations here
    $file = fopen($filename, 'r');
    // Other file handling operations...
    fclose($file);
} else {
    echo 'File does not exist';
}