What potential pitfalls should be considered when working with file handling in PHP?

One potential pitfall when working with file handling in PHP is not properly handling errors that may occur during file operations, such as opening, reading, writing, or closing files. It's important to check for errors and handle them gracefully to prevent unexpected behavior or data loss. You can use functions like `file_exists()`, `is_readable()`, `is_writable()`, `fopen()`, `fwrite()`, and `fclose()` to perform these checks and handle errors accordingly.

$filename = 'example.txt';

// Check if the file exists and is readable
if (file_exists($filename) && is_readable($filename)) {
    // Open the file for reading
    $file = fopen($filename, 'r');

    // Check if the file was opened successfully
    if ($file) {
        // Read the file contents
        $content = fread($file, filesize($filename));

        // Close the file
        fclose($file);

        echo $content;
    } else {
        echo 'Error opening the file.';
    }
} else {
    echo 'File does not exist or is not readable.';
}