I am using wamp server. I try to write into the file but it is giving such error: "Warning: fwrite() expects parameter 1 to be resource, boolean given ". How can I solve it?
$file = 'file.txt';
if (($fd = fopen($file, "a") !== false)) {
fwrite($fd, 'message to be written' . "\n");
fclose($fd);
}
Move parentheses:
if (($fd = fopen($file, "a")) !== false) {
fwrite($fd, 'message to be written' . "\n");
fclose($fd);
}
Your code assigned the result of (fopen($file, "a") !== false)
(i.e. a boolean value) to $fd
.
Do one thing at a time, because there is no rush and enough space:
$file = 'file.txt';
$fd = fopen($file, "a");
if ($fd) {
fwrite($fd, 'message to be written' . "\n");
fclose($fd);
}
Especially make the code more easy in case you run into an error message.
Also know your language: A resource in PHP evaluates true, always.
And know your brain: A double negation is complicated, always.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With