PHP fwrite() 期望参数 1 是资源,给定布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30783047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP fwrite() expects parameter 1 to be resource, boolean given
提问by Peter
I am getting
我正进入(状态
Warning: fwrite() expects parameter 1 to be resource, boolean given
I have below given code
我在下面给出了代码
$data ="<html><table><tr><td>test</td></tr></table></html>";
$fileName="test.xls";
$file=fopen($path.$fileName,"w");
if(fwrite($file, $data)){
echo $fileName;
}
How can I rewrite the code to fix this?
我怎样才能重写代码来解决这个问题?
回答by Blizz
The problem here is not the upgrade in PHP version, but the fact that it failed to actually open the file in question.
这里的问题不是 PHP 版本的升级,而是它未能实际打开有问题的文件。
fopen
returns either a resource
on success or false
on failure:
fopen
resource
成功或false
失败时返回 a :
$data ="<html><table><tr><td>test</td></tr></table></html>";
// Make sure we have a / or \ between the path and the filename
$fileName = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . "test.xls";
$file = fopen($fileName,"w");
if ($file === false) {
echo "opening '$fileName' failed";
exit;
}
if (fwrite($file, $data)){
echo $fileName;
}
So you need to figure out why it can't open it in the first place. Perhaps a problem with your $path
? Does it end with a \
(or /
on linux)? Does the actual path exist. Is it actually set (given that it wasn't added to your code here it might just as well be empty as far as we know)
所以你首先需要弄清楚为什么它不能打开它。也许是你的问题$path
?它是否以\
(或/
在 linux 上)结尾?实际路径是否存在。它是否真的设置了(假设它没有添加到您的代码中,据我们所知,它也可能是空的)