fwrite()错误:提供的参数不是有效的流资源.为什么?

Ale*_*lex 1 php warnings

我有这个代码:

$file = '/path/to/file.txt';

if (($fd = fopen($file, "a") !== false)) {
    fwrite($fd, 'message to be written' . "\n");
    fclose($fd);
}
Run Code Online (Sandbox Code Playgroud)

为什么我会收到以下警告?

fwrite(): supplied argument is not a valid stream resource
fclose(): supplied argument is not a valid stream resource
Run Code Online (Sandbox Code Playgroud)

gen*_*sis 7

你的if语句错了.尝试

if (($fd = fopen($file, "a")) !== false) {
Run Code Online (Sandbox Code Playgroud)

因为你的一个就像

$fd = fopen($file, "a") !== false
Run Code Online (Sandbox Code Playgroud)