首页 > 后端开发 > php教程 > php 生成html文件方法_PHP教程

php 生成html文件方法_PHP教程

WBOY
发布: 2016-07-20 11:06:23
原创
874 人浏览过

下面一段生成html文件类是一款用php的fopen生成html 静态文件的方法好,好了费话不说多了我们来看看吧。

下面一段生成html文件类是一款用php的fopen生成html 静态文件的方法好,好了费话不说多了我们来看看吧。

class html
{
    var $dir;
    var $rootdir;
    var $name;
    var $dirname;
    var $url;
    var $time;
    var $dirtype;
    var $nametype;

    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            {
                $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 geturlquery($url='')
    {
        $this->url=$url?$url:$this->url;

        $durl=parse_url($this->url);
        $durl=explode("&",$durl[query]);
        foreach($durl as $surl)
        {
          $gurl=explode("=",$surl);
          $eurl[]=$gurl[1];
        }
        return join("_",$eurl);
    }

    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().'.htm';
        $this->name=$this->dir.'/'.$filename;
        break;

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

        case 'query':
        $this->name=$this->dir.'/'.$this->geturlquery().'.htm';
        break;

        case 'namequery':
        $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->geturlquery().'.htm';
        break;

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

        }
        return $this->name;
    }

    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;

        $content=file($this->url) or die("Failed to open the url ".$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()
     * 删除目录
     * @param $file 目录名(不带/)
     * @return
     */
     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);
            }
        }
    }

}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445042.htmlTechArticle下面一段生成html文件类是一款用php的fopen生成html 静态文件的方法好,好了费话不说多了我们来看看吧。 下面一段生成html文件类是一款用...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板