php静态文件生成类实例分析

yipeiwu_com5年前PHP代码库

本文实例讲述了php静态文件生成类。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:
defined('phpjb51') or die(header("http/1.1 403 not forbidden"));
          
class include_createstatic            
{           
               
    private $htmlpath = '';           
    private $path = '';           
    public $monthpath = '';           
    private $listpath = '';           
    private $content = '';           
    private $filename = '';           
    private $extname = '.html';           
               
    public function createhtml($type,$desname,$content)           
    {           
        $this->htmlpath = getappinf('htmlpath');           
        if (!file_exists($this->htmlpath))           
        {           
            @mkdir($this->htmlpath);           
        }           
        $this->path = $this->htmlpath.$this->monthpath.'/';           
        if (!file_exists($this->path))           
        {           
            @mkdir($this->path);           
        }           
        $this->listpath = $this->htmlpath.'list/';           
        if (!file_exists($this->listpath))           
        {           
            @mkdir($this->listpath);           
        }           
        switch ($type)           
        {           
            case 'index':           
                $this->filename = $desname;           
                break;           
            case 'list':           
                $this->filename = $this->listpath.$desname;           
                break;           
            case 'view':           
                $this->filename = $this->path.$desname;           
                break;           
        }           
        $this->filename .= $this->extname;           
        $this->content = $content;           
    }           
               
    public function write()           
    {           
        $fp=fopen($this->filename,'wb');           
        if (!is_writable($this->filename))           
        {           
            return false;           
        }           
        if (!fwrite($fp,$this->content))           
        {           
            return false;           
        }           
        fclose($fp);           
        return $this->filename;           
    }           
}     
//方法二
if(file_exists("./index.htm"))//看静态index.htm文件是否存在
{
$time=time();
//文件修改时间和现在时间相差?的话,直接导向htm文件,否则重新生成htm
if(time-filemtime("./index.htm")< 600)
{
header("location:classhtml/main.htm");
}
}
//在你的开始处加入ob_start();
ob_start();
//首页内容,就是你的动态部分了
//在结尾加入ob_end_clean(),并把本页输出到一个变量中
$temp=ob_get_contents();
ob_end_clean();
//写入文件
$fp=fopen("./index.htm",'w');
fwrite(fp,temp) or die('写文件错误');
//echo"生成html完成!";

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP 页面跳转到另一个页面的多种方法方法总结

一、用HTTP头信息 也就是用PHP的HEADER函数。PHP里的HEADER函数的作用就是向浏览器发出由HTTP协议规定的本来应该通过WEB服务器的控制指令,例如声明返回信息的类型("...

php接口隔离原则实例分析

本文实例讲述了php接口隔离原则。分享给大家供大家参考,具体如下: 使用多个专门的接口比使用单一的总接口要好。 一个类对另外一个类的依赖性应当是建立在最小的接口上的。 一个接口代表一个角...

PHP使用strtotime计算两个给定日期之间天数的方法

本文实例讲述了PHP使用strtotime计算两个给定日期之间天数的方法。分享给大家供大家参考。具体分析如下: PHP的strtotime函数用于将任何英文文本的日期时间描述解析为Uni...

PHP面向对象程序设计之对象生成方法详解

本文实例讲述了PHP面向对象程序设计之对象生成方法。分享给大家供大家参考,具体如下: 对象 看个例子 <?php abstract class Employee { /...

PHP的时间戳与具体时间转化的简单实现

三个内置函数: time() //获取UNIX系统时间戳 mktime(hour,minute,second,month,day,year) //将指定时间转化为时间戳 d...