我正在尝试为php网页创建评论区域。
我正在关注this视频(我只关注代码,因为我不知道创建者的语言)并且代码似乎不起作用。
这是代码的PHP位(已修改),这是运行时错误告诉我出错的地方。我评论了出错的行:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$url = $_POST["url"];
$message = $_POST["message"];
$post = $_POST["post"];
if($post){
$write = fopen("../database/fnaticcomments.txt", "a+");
fwrite($write, "<b>$name<br>$email<br>$url<br></b>$message<br> ");
fclose($write);
$read = fopen("../database/fnaticcomments.txt", "r+t");
echo "All comments:";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
// $read = fopen("../database/fnaticcomments.txt", "r+t");
echo "All comments:";
// while(!feof($read)){
// echo fread($read, 1024);
}
fclose($read);
}
?>
如果有人可以帮助我,我将不胜感激。
*我得到的错误是:
(1)警告:fopen(../ database / fnaticcomments.txt)[function.fopen]:无法打开流:第95行/home/delpilam/public_html/firstProject/php/fnaticpage.php中的权限被拒绝
(2)警告:feof()期望参数1是资源,第98行/home/delpilam/public_html/firstProject/php/fnaticpage.php中给出的布尔值
(3)警告:fread()期望参数1为资源,布尔值在第99行的/home/delpilam/public_html/firstProject/php/fnaticpage.php中给出
答案 0 :(得分:2)
在其他情况下,如果没有添加评论,则尝试读取不存在的文件,添加file_exists检查
试试这个:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$url = $_POST["url"];
$message = $_POST["message"];
$post = $_POST["post"];
if($post){
$write = fopen("../database/fnaticcomments.txt", "a+");
fwrite($write, "<b>$name<br>$email<br>$url<br></b>$message<br> ");
fclose($write);
$read = fopen("../database/fnaticcomments.txt", "r+t");
echo "All comments:";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
if(file_exists("../database/fnaticcomments.txt")) {
$read = fopen("../database/fnaticcomments.txt", "r+t");
echo "All comments:";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
} else {
echo "No comments";
}
}
?>