What are the potential pitfalls of using fopen(), fwrite(), and fclose() functions in PHP when writing to a file?

One potential pitfall of using fopen(), fwrite(), and fclose() functions in PHP when writing to a file is not properly handling errors that may occur during the file operations. To mitigate this, it is important to check the return values of these functions for errors and handle them accordingly. Additionally, not closing the file properly after writing to it can lead to resource leaks.

<?php
$filename = "example.txt";
$data = "Hello, World!";

$file = fopen($filename, "w");
if ($file === false) {
    die("Error opening file");
}

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

if (fclose($file) === false) {
    die("Error closing file");
}
?>