What are common pitfalls when using PHP for file handling?

One common pitfall when using PHP for file handling is not properly checking for errors when opening, reading, writing, or closing files. To avoid this issue, always use error handling techniques such as checking the return value of file handling functions and using try-catch blocks to catch exceptions.

$file = fopen("example.txt", "r") or die("Unable to open file!");
try {
    // Read or write operations here
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
fclose($file);