My php should write a text file to the webhost. I have a ‘success’ page, when the text file is written. Up to now I did not have an ‘unsuccessful’ message.
If, for any reason, the text file cannot be written, I would like to display a message.
From php.net I see:
fwrite() returns the number of bytes written, or FALSE
on error.
fopen() Returns a file pointer resource on success, or FALSE
on failure
I can’t see how to provoke an fwrite error, but I can provoke an fopen error, by using a nonexistent path. Sure enough , this jumps to my ‘unsuccessful’ message.
$path = "/home/myusername/public_html/nopath/";
$fp = fopen($path . $newname, 'w');
if ($fp === false) {
header("Location: email_unsuccessful.php");
}
else {
header("Location: email_success.php");}
//endif;
fwrite($fp, $body);
fclose($fp);
?>
Do you see any reason why the same should not work with fwrite??
This way, if fwrite() fails for any reason, the user will get a message and can try to send again.