php生成静态类html文件

<?php
/********************************/
/* Title: php生成静态类html文件 */
/* CreateTime: 2014.1.24 */
/* Author :  zhuoyw        */
/********************************/
class html  
{  
    var $dir;        //dir for the htmls(without/)  
    var $rootdir;    //root of html files(without/):html  
    var $name;       //html文件存放路径  
    var $dirname;    //指定的文件夹名称  
    var $url;        //获取html文件信息的来源网页地址  
    var $time;       //html文件信息填加时的时间  
    var $dirtype;    //目录存放方式:year,month,,,,  
    var $nametype;   //html文件命名方式:name  


    function html($nametype='name',$dirtype='year',$rootdir='html')  
    {  
        $this->setvar($nametype,$dirtype,$rootdir);  
    }  


    function setvar($nametype='name',$dirtype='year',$rootdir='html')  
    {  
      $this->rootdir=$rootdir;  
      $this->dirtype=$dirtype;  
      $this->nametype=$nametype;  
    }  


    function createdir($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);  
                }  
            }  
        }  
    }  


    function getdir($dirname='',$time=0)  
    {  
        $this->time=$time?$time:$this->time;  
        $this->dirname=$dirname?$dirname:$this->dirname;  


        switch($this->dirtype)  
        {  
        case 'name':  
        if(empty($this->dirname))  
           $this->dir=$this->rootdir;  
        else  
           $this->dir=$this->rootdir.'/'.$this->dirname;  
        break;  
        case 'year':  
        $this->dir=$this->rootdir.'/'.date("Y",$this->time);  
        break;  


        case 'month':  
        $this->dir=$this->rootdir.'/'.date("Y-m",$this->time);  
        break;  


        case 'day':  
        $this->dir=$this->rootdir.'/'.date("Y-m-d",$this->time);  
        break;  
        }  


        $this->createdir();  


        return $this->dir;  
    }  


    function geturlname($url='')  
    {  
        $this->url=$url?$url:$this->url;  


        $filename=basename($this->url);  
        $filename=explode(".",$filename);  
        return $filename[0];  
    }  


    function getname($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)  
        {  
        case 'name':  
        $filename=$this->geturlname().'.html';  
        $this->name=$this->dir.'/'.$filename;  
        break;  


        case 'time':  
        $this->name=$this->dir.'/'.$this->time=$time?$time:$this->time.'.html';  
        break;  


        case 'nametime':  
        $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->time.'.html';  
        break;  


        }  
        return $this->name;  
    }  
//生成静态html
    function createhtml($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) or die("Failed to open the url ".$this->url." !");;  
//关键步---用file读取$this->url  
        $content=join("",$content);  
        $fp=@fopen($this->name,"w") or die("Failed to open the file ".$this->name." !");  
        if(@fwrite($fp,$content))  
        return true;  
        else  
        return false;  
        fclose($fp);  
    }  
//删除生成的静态文件
    function deletehtml($url='',$time=0,$dirname='')  
    {  
        $this->url=$url?$url:$this->url;  
        $this->time=$time?$time:$this->time;  


        $this->getname();  


        if(@unlink($this->name))  
        return true;  
        else  
        return false;  
    }  


    //删除目录   
     function deletedir($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);  
                return true;  
            }else{  
                unlink($file);  
            }  
        }  
    }  



$time=date("YmdHis");
$html = new html();
$html->html($nametype='name',$dirtype='year',$rootdir='html');
$html->getdir($dirname='',$time);
//$html->createdir($dir='file');
$html->createhtml($url='addPro.html',$time,$dirname='',$htmlname='');
//$html->deletehtml($url='',$time,$dirname='') ;
//$html->deletedir('html');


?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值