Kenny 2007-8-1 22:18
WEB开发源代码:PHP生成静态页面的类
<p><font color="#cc5233">以下是引用片段:<br><span style="font-size: 10pt;"><font color="#000000">class html <br>{ <br>var $dir; //dir for the htmls(without/) <br>var $rootdir; //root of html files(without/):html <br>var $name; //html文件存放路径 <br>var $dirname; //指定的文件夹名称 <br>var $url; //获取html文件信息的来源</font><a class="bluekey" href="http://homepage.yesky.com/" target="_blank"><u><font color="#800080">网页</font></u></a><font color="#000000">地址 <br>var $time; //html文件信息填加时的时间 <br>var $dirtype; //目录存放方式:year,month,,,, <br>var $nametype; //html文件命名方式:name <br><br>function html($nametype='name',$dirtype='year',$rootdir='html') <br><br><br>function setvar($nametype='name',$dirtype='year',$rootdir='html') <br><br><br>function createdir($dir='') <br>{ <br>$this->dir=$dir?$dir:$this->dir; <br><br>if (!is_dir($this->dir)) <br>{ <br>$temp = explode('/',$this->dir); <br>$cur_dir = ''; <br>for($i=0;$i { <br>$cur_dir .= $temp[$i].'/'; <br>if (!is_dir($cur_dir)) <br><br>} <br>} <br>} <br><br>function getdir($dirname='',$time=0) <br>{ <br>$this->time=$time?$time:$this->time; <br>$this->dirname=$dirname?$dirname:$this->dirname; <br><br>switch($this->dirtype) <br>{ <br>case 'name': <br>if(empty($this->dirname)) <br>$this->dir=$this->rootdir; <br>else <br>$this->dir=$this->rootdir.'/'.$this->dirname; <br>break; <br>case 'year': <br>$this->dir=$this->rootdir.'/'.date("Y",$this->time); <br>break; <br><br>case 'month': <br>$this->dir=$this->rootdir.'/'.date("Y-m",$this->time); <br>break; <br><br>case 'day': <br>$this->dir=$this->rootdir.'/'.date("Y-m-d",$this->time); <br>break; <br>} <br><br>$this->createdir(); <br><br>return $this->dir; <br>} <br><br>function geturlname($url='') <br>{ <br>$this->url=$url?$url:$this->url; <br><br>$filename=basename($this->url); <br>$filename=explode(".",$filename); <br>return $filename[0]; <br>} <br><br>function geturlquery($url='') <br>{ <br>$this->url=$url?$url:$this->url; <br><br>$durl=parse_url($this->url); <br>$durl=explode("&",$durl[query]); <br>foreach($durl as $surl) <br><br>return join("_",$eurl); <br>} <br><br>function getname($url='',$time=0,$dirname='') <br>{ <br>$this->url=$url?$url:$this->url; <br>$this->dirname=$dirname?$dirname:$this->dirname; <br>$this->time=$time?$time:$this->time; <br><br>$this->getdir(); <br><br>switch($this->nametype) <br>{ <br>case 'name': <br>$filename=$this->geturlname().'.htm'; <br>$this->name=$this->dir.'/'.$filename; <br>break; <br><br>case 'time': <br>$this->name=$this->dir.'/'.$this->time.'.htm'; <br>break; <br><br>case 'query': <br>$this->name=$this->dir.'/'.$this->geturlquery().'.htm'; <br>break; <br><br>case 'namequery': <br>$this->name=$this->dir.'/'.$this->geturlname().'-'.$this->geturlquery().'.htm'; <br>break; <br><br>case 'nametime': <br>$this->name=$this->dir.'/'.$this->geturlname().'-'.$this->time.'.htm'; <br>break; <br><br>} <br>return $this->name; <br>} <br><br>function createhtml($url='',$time=0,$dirname='',$htmlname='') <br>{ <br>$this->url=$url?$url:$this->url; <br>$this->dirname=$dirname?$dirname:$this->dirname; <br>$this->time=$time?$time:$this->time; <br>//上面保证不重复地把变量赋予该类成员 <br>if(empty($htmlname)) <br>$this->getname(); <br>else <br>$this->name=$dirname.'/'.$htmlname; //得到name <br><br>$content=file($this->url) or die("Failed to open the url ".$this->url." !");; <br><br>///////////////关键步---用file读取$this->url <br><br><br>$content=join("",$content); <br>$fp=@fopen($this->name,"w") or die("Failed to open the file ".$this->name." !"); <br>if(@fwrite($fp,$content)) <br>return true; <br>else <br>return false; <br>fclose($fp); <br>} <br>/////////////////以name为名字生成html <br><br>function deletehtml($url='',$time=0,$dirname='') <br>{ <br>$this->url=$url?$url:$this->url; <br>$this->time=$time?$time:$this->time; <br><br>$this->getname(); <br><br>if(@unlink($this->name)) <br>return true; <br>else <br>return false; <br>} <br><br>/** <br>* function::deletedir() <br>* 删除目录 <br>* @param $file 目录名(不带/) <br>* @return <br>*/ <br>function deletedir($file) <br>{ <br>if(file_exists($file)) <br>{ <br>if(is_dir($file)) <br>{ <br>$handle =opendir($file); <br>while(false!==($filename=readdir($handle))) <br><br>closedir($handle); <br>rmdir($file); <br>return true; <br>}else <br>} <br>} <br><br>} <br>?></font></span></font><div>