【问题标题】:PHP fopen not workingPHP fopen 不工作
【发布时间】:2015-04-29 07:26:14
【问题描述】:

我只是想不通,我一直无法创建组。

我正在尝试做的是您提交一个论坛,它会创建一个包含一些信息的文件。如果组名已经存在(文件)然后告诉他们。虽然我不断收到我所说的“无法创建组”作为我的死亡信息。

这是 PHP 部分:

<?php
if($_POST['ownername'] && $_POST['groupname']){

$ownername = htmlspecialchars($_POST["ownername"]);
$groupname = htmlspecialchars($_POST["groupname"]);

echo 'Owner is ' . $ownername . ' and the group is ' . $groupname;
$groupfile = '/groups/' . $groupname . '.txt';

if(file_exists($groupfile) == false){
    $newgroup = fopen($groupfile, 'w') or die("Unable to create group");
    $txt = "Users:" . $ownername;
    fwrite($newgroup, $txt);
    fclose($myfile);
    echo "<font style='color:green'>The group has been created! You may access it <a href='chat.php?group=" . $groupname . "'>here</a></font>";
} else {
    echo "<font style='color:red'>The group name is taken, please use another name or wait for it to be released</font>";
}
}
?>

那么这是我的 HTML 部分:

<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<input type="text" name="ownername" placeholder="Username"/>
<input type="text" name="groupname" placeholder="Group Name"/>
<input type="submit" value="Create"/>
</form>

感谢您的任何帮助,非常感谢!

【问题讨论】:

  • 文件的文件权限是什么?文件的 is_writable() 是否返回 true?
  • 我认为你在绝对路径上犯了一个错误。您要写入服务器或站点的根文件夹吗?
  • 您的文件系统上的 /groups/... 中是否确实有该文件?或者它是来自您的 webroot 目录的路径?
  • @vvanadiam 我会尝试使用文件位置

标签: php file


【解决方案1】:

如果文件名尚不存在,请使用'x' 而不是'w' 创建文件并允许写入。

$newgroup = fopen($groupfile, 'x');

【讨论】:

  • @Master:同样的错误信息(来自die)?您是否有权访问要在其中创建新文件的目录?由于Manual 它应该可以工作。手册说:'x' - 创建和打开仅供书写
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
相关资源
最近更新 更多