【问题标题】:PHP Code Error fopen not opening and writingPHP代码错误fopen无法打开和写入
【发布时间】:2014-05-18 20:24:22
【问题描述】:

当我运行这个 PHP 代码时,它应该存储注释,但它并没有像它应该做的那样在 cmets.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>

您可以在我的网站here 上查看此演示。

【问题讨论】:

    标签: javascript php jquery html web-services


    【解决方案1】:

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

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

    // allow any operation for any user    
    chmod("comments.txt", 0777);
    

    【讨论】:

    • cmets.txt 文件是否归 www-data 所有,如果不是,您需要更改为 www-data 用户,以便 php 可以覆盖它。
    • 它的 rw-r--r-- 权限
    【解决方案2】:
    if($_POST['content']!=null){
    

    使用此行处理您所追求的内容的首选(或宣扬)方式是:

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

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

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

    从末尾删除"&lt;/hr&gt;",它会修复它,如下所示:

    fwrite($fp,$_POST['content']);
    

    【讨论】:

    • 已更新解决方案,如果文件权限设置正确。
    猜你喜欢
    • 2016-05-06
    • 2015-01-19
    • 2013-04-25
    • 2011-08-16
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    相关资源
    最近更新 更多