关闭。这个问题需要debugging details .它目前不接受答案。
想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。
6年前关闭。
Improve this question
我正进入(状态
Warning: fwrite() expects parameter 1 to be resource, boolean given
我在下面给出了代码
$data ="<html><table><tr><td>test</td></tr></table></html>";
$fileName="test.xls";
$file=fopen($path.$fileName,"w");
if(fwrite($file, $data)){
echo $fileName;
}
我怎样才能重写代码来解决这个问题?
最佳答案
这里的问题不是 PHP 版本的升级,而是它未能实际打开有问题的文件。
fopen
返回 resource
成功或 false
失败时:
$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;
}
所以你首先需要弄清楚为什么它不能打开它。也许你的
$path
有问题?它是否以 \
结尾(或 /
在 linux 上)?实际路径是否存在。它是否真的设置了(鉴于它没有添加到您的代码中,据我们所知,它也可能是空的)
关于PHP fwrite() 期望参数 1 是资源,给定 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30783047/