当前位置: 移动技术网 > IT编程>开发语言>PHP > php文件操作之小型留言本实例

php文件操作之小型留言本实例

2018年06月16日  | 移动技术网IT编程  | 我要评论

本文实例讲述了php文件操作之小型留言本。分享给大家供大家参考。具体如下:

index.php文件如下:

<?php 
$path = "db/"; //定义路径 
$dr = opendir($path); //打开目录 
while($filen = readdir($dr)) //循环读取目录中的文件 
{ 
  if($filen != "." and $filen != "..") 
  { 
    $fs = fopen($path.$filen, "r"); 
    echo "<b>标题:</b>".fgets($fs)."<br>"; 
    echo "<b>作者:</b>".fgets($fs)."<br>"; 
    echo "<b>内容:</b><pre>".fread($fs, filesize($path.$filen))."</pre>";  
    echo "<hr>"; 
    fclose($fs); 
  } 
} 
closedir($dr) //关闭目录 
?> 

post.php文件如下:

<?php 
$path = "db/"; 
$filename = "s".date("ymdhis").".dat"; 
$fp = fopen($path.$filename, "w"); 
fwrite($fp, $_post["title"]."/n"); 
fwrite($fp, $_post["author"]."/n"); 
fwrite($fp, $_post["content"]."/n"); 
fclose($fp); 
echo "留言发表成功!"; 
echo "<a href="index.php" mce_href="index.php">返回首页</a>"; 
?> 

<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<title>发表新的留言</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<h1><p align="center">发表新的留言</p></h1>
<form name="form1" method="post" action="post.php">
 <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
   <td>标题</td>
   <td><input name="title" type="text" id="title" size="50"></td>
  </tr>
  <tr>
   <td>作者</td>
   <td><input name="author" type="text" id="author" size="20"></td>
  </tr>
  <tr>
   <td>内容</td>
   <td><textarea name="content" cols="50" rows="10" id="content"></textarea></td>
  </tr>
 </table>
 <p align="center">
  <input type="submit" value="submit">
  <input type="reset" value="reset">
</p>
</form>
</body>
</html>

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网