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

I'm having a problem with PHP when trying to use the fopen to write lines of text inside of a textarea to a .txt file. Here's my code, hopefully someone can help!

<?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>

I've tried everything that could come up but I can't seem to find the right solution... I tried making and using a function called sendData() but that didn't work either. Any help anyone can provide is greatly appreciated, thanks!

  1. check if the file exist
  2. check if permission is granted to write the file
  3. sendData() its php and you call it from html
  4. you forgot declare this variable = $u when you call the function

you can do something like this:

<?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>