当前位置: 代码迷 >> PHP >> PHP把文本文档的内容按行输出来以后,如何按下图的形式显示出来
  详细解决方案

PHP把文本文档的内容按行输出来以后,如何按下图的形式显示出来

热度:99   发布时间:2016-04-28 16:51:31.0
PHP把文本文档的内容按行输出来以后,怎么按下图的形式显示出来。



<?php
//写入文本
$myfile = fopen("newfile.txt", "a+") or die("Unable to open file!");
$username=@$_POST[username];
fwrite($myfile, $username);

$password=@$_POST[password]."<br>";
fwrite($myfile, $password);

//读取文件
$myfile = fopen("newfile.txt", "r") or die("Unable to open file!");
// 输出单行直到 end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>


<html>
<header>
</header>
<body>
<form action="index.php" method="post">
用户名:<input type="text" name="username"/><br><br>
密码:<input type="text" name="password" /><br><br>
<input type="submit" value="提交"/>
</form>
<table border="0">
<tr><td>用户名</td><td>密码</td></tr>
<tr> 
<td><?php echo $username ?></td>
<td><?php echo $password ?></td>
<td>删除</td>
</tr>
</table>
</body>
</html>

------解决思路----------------------
form.php

<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<body>
<form name="form" action="form-action.php" method="post">
    <input name="username" type="text" />
    <input name="password" type="text"/>
    <input type="submit" name="submit" value="提交"/>
</form>
<?php
$path = 'D:\wamp\www\a.txt';
$f = fopen($path, "r");
echo "用户名"." " ."密码"." 操作<br/>";
while(!feof($f))
{
    if (!feof($f)){
        $buffer = fgets($f, 4096);
        $arr = explode("
------解决思路----------------------
",$buffer);
        if(count($arr) > 1){
            echo $arr[0]." " . $arr[1]." 删除<br/>";
        }
    }
}
fclose($f);
?>
</body>
</html>


form-action.php

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$path = 'D:\wamp\www\a.txt';
$fp = fopen($path,"a");
fwrite($fp,$username."
------解决思路----------------------
".$password."\r\n");
fclose($fp);
header("Location:form.php");
?>

用户名 密码 操作
11 22 删除
33 44 删除
55 66 删除
ss dd 删除
s sss 删除