一天一篇之php学习篇4

时间:2014-01-16 20:31:42   收藏:0   阅读:291

今天简单的学习了下php操作目录方法,做个总结:

opendir()//打开目录 

readdir()//读取目录

mkdir()//创建目录

rmdir()//删除目录

closedir()//关闭目录

fopen()//打开文件

fwrite()//写入文件

简单的留言本功能,使用的都是操作文件夹目录/文件功能:

post.html

bubuko.com,布布扣
<body>
<form name="frm1" method="post" action="post.php">
    <label>主题:<input type="text" name="title" /></label>
    <label>作者:<input type="text" name="author" /></label>
    <label>内容:<input type="text" name="content" /></label>
    <input type="submit" name="submit" />
</form>
</body>
bubuko.com,布布扣

post.php(负责写入数据)

bubuko.com,布布扣
//写入数据
<?php
$path = "DB/";//目录
$fsn = "s".date(‘YmdHis‘).".dat";//文件名
$fp = fopen($path.$fsn,"w");//打开文件
fwrite($fp,$_POST["title"]."\n");//写入数据
fwrite($fp,$_POST["author"]."\n");
fwrite($fp,$_POST["content"]."\n");
fclose($fp);
//跳转
echo "<script>alert(‘sucess‘);window.location = ‘index.php‘;</script>";
?>
bubuko.com,布布扣

index.php(负责读取数据)

bubuko.com,布布扣
<?php
$path = "DB/";
$dr = opendir($path);
while($filen = readdir($dr))
{
   if($filen != "." and $filen != "..")
  {
       $fs = fopen($filen,"r");
      echo "<b>标题:</b".fgets($fs)."<br>"; 
      echo "<b>作者:</b".fgets($fs)."<br>";
      echo "<b>内容:</b".fread($fs,fliesize($path.$filen))."<br>";
      fclose($fs);
  }      
}
closedir($path);
bubuko.com,布布扣

原文:http://www.cnblogs.com/dbxh/p/3518434.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!