将文本中的文本保存在文件中

时间:2018-04-10 19:10:47

标签: php html file session

我目前正在为网站制作一个生物部分,到目前为止,我有办法编辑个人资料"。我想要发生的是插入的文本被保存到我已经创建的文件中。这是我目前使用他们的姓名的代码。

个人资料编辑页面(editprofile.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';
?>
<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">
        <textarea style="position: absolute;    top: 75px; left: 132.5px; width: 450px; height: 87px;"></textarea>
    </div>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>

显示配置文件页面(profile.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';
?>
<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">
        <iframe width=25% height=9.5% src="getbio.php" background-color='white' style=" position: absolute; top: 75px; left: 132.5px;"></iframe>
    </div>
    <p><a href="editprofile.php" style="color: gray;">Edit Your Profile</a></p>
</body>
<footer>
<div class="status">Currently logged in as <?php echo $username ?></div>
</footer>
</html>

从文件中获取生物文本的部分(getbio.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'];
$bioFile = "bio/" . $username . ".txt";
if (!file_exists($bioFile))
    $bioFile = 'bio/defaultBio.txt';
?>
<style>body {background-color: #f1f1f1;}
</style><body><?php  echo nl2br( file_get_contents($bioFile) );
?></body>

CSS(main.css)

body {
    margin: 0;
    background: #222;
    font-family: arial;
    font-weight: 500;
}

.container {
    width: 100%;
    margin: 0 auto;
}

header {
  background: #55d6aa;
}

header::after {
  content: '';
  display: table;
  clear: both;
}

nav {
  float: left;
  width: 100%;
}

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  display: inline-block;
  margin-left: 70px;
  padding-top: 10px;
  padding-bottom: 10px;
  position: relative;
}

nav a {
  color: #444;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 14px;
}

nav li:hover {
  color: #000;
}

nav li::before {
  content: '';
  display: block;
  height: 5px;
  background-color: #444;

  position: absolute;
  top: 0;
  width: 0%;

  transition: all ease-in-out 250ms;
}

nav li:hover::before {
  width: 100%;
}
.logout {
  float: right;
  margin-right: 50px;
}
.status {
  position: absolute;
  bottom: 5px;
  right: 5px;
  color: green;
}
.download {
    display: inline-block;
    text-decoration: none;
    list-style: none;
}
.download a{
    color: lime;
    text-decoration:none;
    padding-left: 50px;
}
.download a:hover{
    color: green;
    text-decoration:none;
}
.download li {
    display: inline;
    float: right;
    padding-top: 17.5px;
}

CSS仅用于浏览网站,其他部分仅供参考。我不确定如何将文本从textarea(editprofile.php的一部分)保存到名为/bio的文件夹中的文件。如果有人能帮助我,我真的很感激。

1 个答案:

答案 0 :(得分:0)

代码中缺少的是将编辑页面上输入的数据发送到服务器进行处理和保存的方法。显然,您无法打开尚未创建的文件。

用于向服务器发送数据的html元素是<form>,它与编辑(输入)元素(如<textarea>)进行交互。

我建议你学习一些东西。 Here是一个值得一去的地方。