How reliable is the fwrite function in PHP when writing new files?

The fwrite function in PHP is generally reliable when writing new files. However, it is important to handle possible errors that may occur during the file writing process. One way to ensure reliability is to check the return value of fwrite to confirm that the data was written successfully.

$file = fopen("newfile.txt", "w");
if ($file) {
    $data = "Hello, World!";
    if (fwrite($file, $data) !== false) {
        echo "Data was written successfully.";
    } else {
        echo "Error writing data to file.";
    }
    fclose($file);
} else {
    echo "Error opening file for writing.";
}