What are the potential pitfalls of using the fopen function in PHP to write to a text file?

One potential pitfall of using the fopen function in PHP to write to a text file is not properly handling errors that may occur during the file writing process. It is important to check for errors and handle them gracefully to prevent data loss or corruption. Additionally, not properly closing the file after writing to it can lead to resource leaks and potential issues with file locking.

$file = fopen("example.txt", "w");

if ($file === false) {
    die("Unable to open file for writing.");
}

$data = "Hello, World!";
if (fwrite($file, $data) === false) {
    die("Error writing to file.");
}

fclose($file);