生成静态页面的PHP类
2018-09-07 12:18
复制代码 代码如下:<?php
classhtml
{
var$dir;//dirforthehtmls(without/)
var$rootdir;//rootofhtmlfiles(without/):html
var$name;//html文件存放路径
var$dirname;//指定的文件夹名称
var$url;//获取html文件信息的来源网页地址
var$time;//html文件信息填加时的时间
var$dirtype;//目录存放方式:year,month,,,,
var$nametype;//html文件命名方式:name
functionhtml($nametype=name,$dirtype=year,$rootdir=html)
{
$this->setvar($nametype,$dirtype,$rootdir);
}
functionsetvar($nametype=name,$dirtype=year,$rootdir=html)
{
$this->rootdir=$rootdir;
$this->dirtype=$dirtype;
$this->nametype=$nametype;
}
functioncreatedir($dir=)
{
$this->dir=$dir?$dir:$this->dir;
if(!is_dir($this->dir))
{
$temp=explode(/,$this->dir);
$cur_dir=;
for($i=0;$i<count($temp);$i++)
{
$cur_dir.=$temp[$i]./;
if(!is_dir($cur_dir))
{
@mkdir($cur_dir,0777);
}
}
}
}
functiongetdir($dirname=,$time=0)
{
$this->time=$time?$time:$this->time;
$this->dirname=$dirname?$dirname:$this->dirname;
switch($this->dirtype)
{
casename:
if(empty($this->dirname))
$this->dir=$this->rootdir;
else
$this->dir=$this->rootdir./.$this->dirname;
break;
caseyear:
$this->dir=$this->rootdir./.date(Y,$this->time);
break;
casemonth:
$this->dir=$this->rootdir./.date(Y-m,$this->time);
break;
caseday:
$this->dir=$this->rootdir./.date(Y-m-d,$this->time);
break;
}
$this->createdir();
return$this->dir;
}
functiongeturlname($url=)
{
$this->url=$url?$url:$this->url;
$filename=basename($this->url);
$filename=explode(.,$filename);
return$filename[0];
}
functiongeturlquery($url=)
{
$this->url=$url?$url:$this->url;
$durl=parse_url($this->url);
$durl=explode(&,$durl[query]);
foreach($durlas$surl)
{
$gurl=explode(=,$surl);
$eurl[]=$gurl[1];
}
returnjoin(_,$eurl);
}
functiongetname($url=,$time=0,$dirname=)
{
$this->url=$url?$url:$this->url;
$this->dirname=$dirname?$dirname:$this->dirname;
$this->time=$time?$time:$this->time;
$this->getdir();
switch($this->nametype)
{
casename:
$filename=$this->geturlname()..htm;
$this->name=$this->dir./.$filename;
break;
casetime:
$this->name=$this->dir./.$this->time..htm;
break;
casequery:
$this->name=$this->dir./.$this->geturlquery()..htm;
break;
casenamequery:
$this->name=$this->dir./.$this->geturlname().-.$this->geturlquery()..htm;
break;
casenametime:
$this->name=$this->dir./.$this->geturlname().-.$this->time..htm;
break;
}
return$this->name;
}
functioncreatehtml($url=,$time=0,$dirname=,$htmlname=)
{
$this->url=$url?$url:$this->url;
$this->dirname=$dirname?$dirname:$this->dirname;
$this->time=$time?$time:$this->time;
//上面保证不重复地把变量赋予该类成员
if(empty($htmlname))
$this->getname();
else
$this->name=$dirname./.$htmlname;//得到name
$content=file($this->url)ordie(Failedtoopentheurl.$this->url.!);;
///////////////关键步---用file读取$this->url
$content=join(,$content);
$fp=@fopen($this->name,w)ordie(Failedtoopenthefile.$this->name.!);
if(@fwrite($fp,$content))
returntrue;
else
returnfalse;
fclose($fp);
}
/////////////////以name为名字生成html
functiondeletehtml($url=,$time=0,$dirname=)
{
$this->url=$url?$url:$this->url;
$this->time=$time?$time:$this->time;
$this->getname();
if(@unlink($this->name))
returntrue;
else
returnfalse;
}
/**
*function::deletedir()
*删除目录
*@param$file目录名(不带/)
*@return
*/
functiondeletedir($file)
{
if(file_exists($file))
{
if(is_dir($file))
{
$handle=opendir($file);
while(false!==($filename=readdir($handle)))
{
if($filename!=.&&$filename!=..)
$this->deletedir($file./.$filename);
}
closedir($handle);
rmdir($file);
returntrue;
}else{
unlink($file);
}
}
}
}
?>
下一篇:php中目录,文件操作详谈