警告:feof()要求参数1为resource,在第62行/volume1/web/comment.php中给出布尔值


Warning: feof() expects parameter 1 to be resource, boolean given in /volume1/web/comment.php on line 62

我只是在youtube和其他网站的帮助下才开始学习代码,我已经遇到了一个问题。这是我的代码:

<form action="" method="post" id="c">
  <label> Name: <br><input type="text" name="name" size="36"></label><br></br>
  <label> Message: <br><textarea cols="35" rows="5" name="mes"></textarea></label><br></br>
  <input type="submit" name="submit" value="Submit" class="texty" >
</form>
<?php
$post = $_POST["post"];
$name = $_POST["name"];
$text = $_POST["mes"];
if ($post) {
    #WRITE DOWN COMMENTS#
    $write = fopen("c.txt", "a+");
    fwrite($write, "<u><b> $name</b></u><br>$text<br></br>");
    fclose($write);
    #DISPLAY COMMENTS#
    $read = fopen("c.txt", "r+t");
    echo "All comments:<br>";
    while (!feof($read)) {   #this line does the error#
        echo fread($read, 1024);
    }
    fclose($read);
}
else{
    #DISPLAY COMMENTS#
    $read = fopen("c.txt", "r+t");
    echo "All comments:<br>";
    while (!feof($read)) {
        echo fread($read, 1024);
    }
    fclose($read);
}
?>

所以我有两个文件,一个是你输入评论并发布的文件,另一个是.txt文件,你输入的内容被粘贴,然后回显到页面上。我认为文件的权限可能有问题,因为如果我只把它放在"r",那么它不会给我错误,但它不会保存我试图发布的内容。。。感谢您的阅读和回复。

如果无法打开文件,则

fopen将返回布尔值(FALSE(。在尝试将$read传递给feof之前,您应该检查它是否为false。然后找出你为什么不能读取文件。

if ($read) {
    ...

您的文件可能由于权限或找不到文件本身而无法读取。你指的是c.txt,但它和PHP在同一个目录中吗?您可以通过getcwd找到PHP所在的目录。

echo getcwd()

此外,如果$_POST['post']有一个值,但表单中没有使用该名称(至少在所示示例中没有(,则表单处理程序只会进入写分支。

谢谢Dan,问题出在提交命令中。一个是提交,另一个是发布,所以它不明白我想做什么。PS:很抱歉回复延迟;_;