使用fopen将文本行写入txt文件php

使用fopen将文本行写入txt文件php,php,html,Php,Html,在尝试使用fopen将textarea中的文本行写入.txt文件时,我遇到了PHP问题。这是我的代码,希望有人能帮忙 <?php session_start(); if (!isset($_SESSION['username'])) { $_SESSION['msg'] = "You must log in first"; header('location: login.php'); } if (isset($_GET['lo

在尝试使用
fopen
textarea
中的文本行写入
.txt
文件时,我遇到了PHP问题。这是我的代码,希望有人能帮忙

<?php
    session_start();
    if (!isset($_SESSION['username'])) {
        $_SESSION['msg'] = "You must log in first";
        header('location: login.php');
    }
    if (isset($_GET['logout'])) {
        session_destroy();
        unset($_SESSION['username']);
        header("location: login.php");
    }
    $username = $_SESSION['username'];
    $file = "extra/" . $username . ".png";
    if (!file_exists($file)) $file = 'extra/defaultProfile.png';
    function sendData($u) {
        $myfile = fopen('bio/' . $username . '.txt', $u);
    }
?>
<html>
    <title> Home Page </title>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <header>
        <div class="container">
            <nav>
                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="downloads.php">Downloads</a></li>
                    <li><a href="chat.php">Chat</a></li>
                    <li><a href="profile.php">Profile</a></li>
                    <li class="logout"><a href="index.php?logout='1'">Logout</a></li>
                </ul>
            </nav>
        </div>
    </header>
    <body>
        <div class="profileimg">
            <img src=<?php echo $file; ?>  width="125" height="125">
        </div>
        <div class="profilename">
            <p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
        </div>
        <div class="biotext">
            <form action="" method="post">
                <textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
                <input type="submit" name="submit" value="submit" formaction="sendData()"/>
            </form>
        </div>
    </body>
    <footer>
        <div class="status">Currently logged in as <?php echo $username ?></div>
    </footer>
</html>

主页
width=“125”height=“125”>

当前登录为
我尝试了所有可能出现的方法,但似乎找不到正确的解决方案。。。我尝试创建并使用一个名为
sendData()
的函数,但也没有成功。非常感谢任何人提供的任何帮助,谢谢

  • 检查文件是否存在
  • 检查是否授予写入文件的权限
  • sendData()是php,您可以从html调用它
  • 调用函数时忘记声明此变量=$u
  • 您可以这样做:

    <?php
      if (!empty($_POST))
      {
        $text_data = $_POST['text_data'];
        $username = $_POST['username'];
        fopen('bio/' . $username . '.txt', $text_data);
      }
    ?>
    <html>
    <title> Home Page </title>
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <header>
        <div class="container">
          <nav>
            <ul>
              <li><a href="index.php">Home</a></li>
              <li><a href="downloads.php">Downloads</a></li>
              <li><a href="chat.php">Chat</a></li>
              <li><a href="profile.php">Profile</a></li>
              <li class="logout"><a href="index.php?logout='1'">Logout</a></li>
            </ul>
          </nav>
        </div>
      </header>
    <body>
      <div class="profileimg">
        <img src=<?php echo $file; ?>  width="125" height="125">
      </div>
      <div class="profilename">
        <p style="position: absolute; top: 8px; left: 130px; color: white; font-size: 30px;"><?php echo $username ?></p>
      </div>
        <div class="biotext">
            <form action="" method="post">
            <textarea name="lines" width=25% height=9.5% background-color='white' style="position: absolute; top: 75px; left: 132.5px;"></textarea>
            <input type="text" name="text_data" value="">
            <input type="hidden" name="username" value="<?php echo $username ?>">
            <input type="submit" name="submit" value="submit"/>
            </form>
        </div>
    </body>
    <footer>
    <div class="status">Currently logged in as <?php echo $username ?></div>
    </footer>
    </html>
    
    
    主页
    
    width=“125”height=“125”>


    看一下文档:
    formaction=“sendData()”
    这是什么?我从来没有使用过formaction属性,但您在调用“sendData()”函数时没有任何参数,也不应该使用:$myfile=fopen('bio/'.$username..txt',w),而不是$myfile=fopen('bio/'.$username..txt',$u)?出于某种原因,这是行不通的。它不会替换文件中的任何文本。。。