<?php $ok = true; if(isset($_POST['title']) && isset($_POST['content'])) //判断变量$_POST['content']和$_POST['title'] { $ok = true; $title = trim($_POST['title']); //获取日志标题 $content = trim($_POST['content']); //获取日志内容 $date = time(); //获取日志时间 $blog_str = $title.'|'.$date.'|'.$content; //将上述内容合并成字符串 $ym = date('Ym',time()); //获取日期中的年和月 $d = date('d',time()); //获取日期中的日 $time = date('His',time()); //获取日期中的时间 $folder = 'contents/'.$ym; //根据年和月设置目录名 $file = $d.'-'.$time.'.txt'; //获取时间和日来设置文件名 $filename = $folder.'/'.$file; $entry = $ym.'-'.$d.'-'.$time; if(file_exists($folder) == false) { if(!mkdir($folder)) { //$ok = false; //$msg = '<font color=red>创建目录异常,添加日志失败</font>'; } } $fp = @fopen($filename,'w'); //打开文件 if($fp) { flock($fp,LOCK_EX); $result = fwrite($fp,$blog_str); //写入文件 $lock = flock($fp, LOCK_UN); fclose($fp); //关闭文件 } if(strlen($result)>0) //判断写入是否成功 { $ok = false; $mag = '日志添加成功,<a href="12-1.php?entry='.$entry.'">查看该日志文章</a>'; echo $mag; } } ?> <html> <head> <title>基于文本的简易BLOG</title> <link rel = "stylesheet" type="text/css" href="/style.css" /> </head> <body> <div id="container"> <div id="header"> <h1>我的BLOG</h1> </div> <div id="title"> ----I have dream .... </div> <div id="left"> <div id="blog_entry"> <div id="blog_title">添加一篇新日志</div> <div id="blog_body"> <div id="blog_date"></div> <table border="1"> <form method="POST" action="12-2.php"> <tr><td>日志标题:</td></tr> <tr><td><input type="text" name="title" size="60"></td></tr> <tr><td>日志内容:</td></tr> <tr><td><textarea name="content" cols="49" rows="10"></textarea></td></tr> <tr><td><input type="submit" value="提交"></td></tr> </form> </table> </div><!-- blog_body --> </div><!-- blog_entry --> </div> <div id="right"> <div id="sidebar"> <div id="menu_title">关于我</div> <div id="menu_body">我是个PHP爱好者</div> </div> </div> <div id="footer"> CopyRight 2016 </div> </div> </body> </html>
转载于:https://my.oschina.net/null7654321/blog/711707