15
$content=$_POST[“content”]; //
获得表单变量
16
//以下建立一文本文档,其值自动计数
17
$countfile="count.txt";
18
if(!file_exists($countfile))
19
{
20
fopen($countfile,"w"); //如果此文件不存在,则自动建立一个
21
}
22
$fp=fopen($countfile,"r");
23
$num=fgets($fp,20);
24
$num=$num+1; //每次其值自动加一
25
fclose($fp);
26
$fp=fopen($countfile,"w");
27
fwrite($fp,$num); //更新其值
28
fclose($fp);
复制代码
//利用上面自动计数的值获得 HTML 的路径$path
$houzui=”.html”;
$path=$num.$houzui;
//这样形成的路径是自动增长的,如 1.html,2.html,3.html……….添加一条新闻便自动加
上 1
//以下用 SQL
语句添加数据至表 news
29
$sql=”insert into news (title,content,path) values (‘”.$title.”’,’”.$content.”’,’”.$path.”’)”;
30
$query=mysql_query($sql);
复制代码
//以下为关键之处,把从表单获得的数据替换模板中的{title},{content}标记
31
$fp=fopen(“model.htm”,”r”) //只读打开模板
32
$str=fread($fp,filesize(“mode.htm”));//读取模板中内容
33
$str=str_replace(“{title}”,$title,$str);
34
$str=str_replace(“{content}”,$content,$str);//替换内容
35
fclose($fp);
36
$handle=fopen($path,”w”); //写入方式打开新闻路径
37
fwrite($handle,$str); //把刚才替换的内容写进生成的 HTML 文件
38
fclose($handle);
复制代码