PHP fwrite() 期望参数 1 为资源,给定布尔值

Pet*_*ter 4 php

我正进入(状态

Warning: fwrite() expects parameter 1 to be resource, boolean given
Run Code Online (Sandbox Code Playgroud)

我有下面给出的代码

$data ="<html><table><tr><td>test</td></tr></table></html>";
$fileName="test.xls";
$file=fopen($path.$fileName,"w");

if(fwrite($file, $data)){       
    echo $fileName;
}
Run Code Online (Sandbox Code Playgroud)

我该如何重写代码来解决这个问题?

Bli*_*izz 5

这里的问题不是 PHP 版本的升级,而是它无法实际打开有问题的文件。

fopenresource成功或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;
}
Run Code Online (Sandbox Code Playgroud)

所以你首先需要弄清楚为什么它无法打开它。也许是你的问题$path?它以 a 结尾吗\(或者/在 linux 上)?实际路径是否存在。它是否真的设置了(鉴于它没有添加到您的代码中,据我们所知,它可能只是空的)