What are some common pitfalls when working with file handling in PHP, and how can they be avoided?

One common pitfall when working with file handling in PHP is not properly checking if a file exists before trying to read or write to it. This can lead to errors or unexpected behavior if the file is not found. To avoid this, always use functions like `file_exists()` or `is_file()` to check if a file exists before attempting any operations on it.

$file_path = 'example.txt';

if (file_exists($file_path)) {
    // Read or write to the file
    $file_contents = file_get_contents($file_path);
    // Do something with the contents
} else {
    echo 'File does not exist.';
}