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

时间:2018-04-11 11:39:59

标签: php html

我在尝试使用fopentextarea.txt文件中写入文本行时遇到问题。这是我的代码,希望有人可以提供帮助!

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

我已经尝试了所有可能出现的但我似乎无法找到正确的解决方案......我尝试制作并使用名为sendData()的函数,但这也不起作用。非常感谢任何人提供的任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

  1. 检查文件是否存在
  2. 检查是否授予了写入文件的权限
  3. sendData()它的php,你从html中调用它
  4. 你忘了在调用函数
  5. 时声明这个变量= $ 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>