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

php自动生成html新闻文件

2016-04-08 14:47 555 查看
<?php
date_default_timezone_set("prc");
//连接数据库
//connectDB

$title=$_REQUEST['title'];
$time=$_REQUEST['time'];
$author=$_REQUEST['author'];
$content=$_REQUEST['content'];
//模板文件路径
$mobanpath="./moban/moban.html";
if(!file_exists($mobanpath)){
die("没有模板文件");
}
//打开模板文件
$fp=fopen($mobanpath,'r');
//读取模板文件
$str=fread($fp,filesize($mobanpath));
//$str=htmlspecialchars($str);
//将接收到的字段,替换模板文件的字段
$str=str_replace("-title-",$title,$str);
$str=str_replace("-time-",$time,$str);
$str=str_replace("-author-",$author,$str);
$str=str_replace("-content-",$content,$str);
//当天新闻文件夹
$foldername=date("Y-m-d");
//文件夹路径
$folderpath="./newslist/".$foldername;
//如果没有这个文件夹就创建一个
if(!file_exists($folderpath)){
mkdir($folderpath);
}
//生成文件名字
$filename=date("H-i-s").".html";
//生成文件路径
$filepath="{$folderpath}/{$filename}";
//判断是否有此文件
if(!file_exists($filepath)){
//没有的话,创建文件
$fp=fopen($filepath,"w");
fwrite($fp,$str);
fclose($fp);
}

//添加到数据库语句
//  sql..........

header("Location:index.php?msg=ok");
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: