<?php
date_default_timezone_set( "Asia/Shanghai");
class TCreateHTML{
var $HTemplate; //模板的文件
var $FileName; //新文件名称
var $HTFilePath;//产生文件的路径
var $ModiString;
var $ReTextArray;//替换信息数组
var $strText=""; //文本内容
var $CrType="1"; //产生文件名称的类型 默认为日期+4位 随机数
var $errorStr; //错误信息!
//********接口函数
//构造模板
function TCreateHTML(){}
//创建文件
function CreatrHtml()
{
//检查模板路径是否合法
if(!$this->CheckPath($this->HTemplate,"0"))
{
return false;
}
//检查新生文件路径是否合法
if(!$this->CheckPath($this->HTFilePath,"1"))
{
return false;
}
$fp=fopen($this->HTemplate,"r"); //只读打开模板
$this->strText=fread($fp,filesize($this->HTemplate));//读取模板中内容
fclose($fp);
//替换文件
$this->ReplaceText();
//生成文件名称
$this->CreateName();
$handle=fopen($this->FileName,"w"); //写入方式打开新闻路径
fwrite($handle,$this->strText); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
return true;
}
function CheckPath($pStr,$type)
{
if($type=="0")
{
if(!file_exists($pStr))
{
$this->errorStr="文件夹路径有误!";
return false;
}
}else
{
$arrPath = explode ("/", $pStr);
$s_Path="";
foreach ($arrPath as $tag) {
if($s_Path=="")
{
$s_Path .= $tag;
}else
{
$s_Path .="/".$tag;
}
if(!file_exists($s_Path))
{
mkdir($s_Path,0777);
}
}
}
return true;
}
//产生文件名称
function CreateName()
{
switch ($this->CrType) {
case 1:
$this->GetFileRand();
break;
case 2:
$this->GetFildName();
break;
default:
$this->GetFileRand();
}
}
//替换文件的内容
function ReplaceText()
{
if (is_array($this->ReTextArray))
{
while (list($key,$value)=each($this->ReTextArray)){
//echo "$key : $value"."<br>";
$this->strText=str_replace($key,$value,$this->strText);
}
}
}
//产生顺序文件名
function GetFildName()
{
if($this->HTFilePath!="")
{
$countfile=$this->HTFilePath."/"."count.txt";
}else
{
$countfile="count.txt";
}
if(!file_exists($countfile))
{
fopen($countfile,"w"); //如果此文件不存在,则自动建立一个
}
$fp=fopen($countfile,"r");
$num=fgets($fp,20);
$num=$num+1; //每次其值自动加一
fclose($fp);
$fp=fopen($countfile,"w");
fwrite($fp,$num); //更新其值
fclose($fp);
$houzui=".html";
$path=$num.$houzui;
if($this->HTFilePath!="")
{
$this->FileName=$this->HTFilePath."/".$path;
}else
{
$this->FileName=$path;
}
}
function GetFileRand()
{
$today = getdate();
$fname=$today["year"];
$fname .= $today["mon"]<10?"0".$today["mon"]:$today["mon"];
$fname .= $today["mday"]<10?"0".$today["mday"]:$today["mday"];
$fname .= $today["hours"]<10?"0".$today["hours"]:$today["hours"];
$fname .= $today["minutes"]<10?"0".$today["minutes"]:$today["minutes"];
$seedstr =split(" ",microtime(),5);
$seed =$seedstr[0]*10000;
srand($seed);
$random =rand(1000,10000);
$random=$fname.$random;
if($this->HTFilePath!="")
{
$this->FileName=$this->HTFilePath."/".$random.".html";
}else
{
$this->FileName=$random.".html";
}
}
}//end class
?>