您的位置:首页 > 编程语言 > PHP开发

一天一篇之php学习篇4

2014-01-14 09:47 417 查看
今天简单的学习了下php操作目录方法,做个总结:

opendir()//打开目录

readdir()//读取目录

mkdir()//创建目录

rmdir()//删除目录

closedir()//关闭目录

fopen()//打开文件

fwrite()//写入文件

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

post.html

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


post.php(负责写入数据)

//写入数据
<?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>";
?>


index.php(负责读取数据)

<?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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: