PHP代码错误,不能打开和写入

PHP Code Error fopen not opening and writing

本文关键字:不能 代码 错误 PHP      更新时间:2023-09-26

当我运行这段PHP代码时,它应该存储评论,但它没有像它应该做的那样在comments.txt中写入任何内容。请找出错误。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<?php
setcookie("username","admin", time()+3600);
setcookie("password","xss", time()+3600);
if($_POST['content']!=null){
$fp= fopen('comments.txt','a');
fwrite($fp,$_POST['content'], "</hr>");
fclose($fp);
}
echo nl2br(file_get_contents('comments.txt'));
?>
<h3>Post Your HTML Code here</h3>
<form action="index.php" method="post">
<textarea name="content" rows="3" cols="100"></textarea>
<br/>
<input type="submit" value="Post" />
</form>
</body>
</html>

你可以在我的网站上看到这个的演示

您是否检查了您的文件是否具有apache写入它的权限?

可以使用chmod修改权限,例如:

// allow any operation for any user    
chmod("comments.txt", 0777);
if($_POST['content']!=null){

处理这一行的首选(或鼓吹)方法是:

if(isset($_POST['content'])) {

对于实际错误,这是因为您为fwrite()使用了错误的参数。在PHP文档中,参数是:

int fwrite ( resource $handle , string $string [, int $length ] )

从末尾去掉"</hr>",它会修复它,像这样:

fwrite($fp,$_POST['content']);
相关文章: