溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用php編寫一個靜態(tài)文件生成類

發(fā)布時間:2021-03-04 17:29:23 來源:億速云 閱讀:118 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)利用php編寫一個靜態(tài)文件生成類,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

php有什么用

php是一個嵌套的縮寫名稱,是英文超級文本預(yù)處理語言,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因為php是開源的,從而使得php經(jīng)久不衰。

具體實現(xiàn)方法如下:

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"))//看靜態(tài)index.htm文件是否存在
{
$time=time();
//文件修改時間和現(xiàn)在時間相差?的話,直接導(dǎo)向htm文件,否則重新生成htm
if(time-filemtime("./index.htm")< 600)
{
header("location:classhtml/main.htm");
}
}
//在你的開始處加入ob_start();
ob_start();
//首頁內(nèi)容,就是你的動態(tài)部分了
//在結(jié)尾加入ob_end_clean(),并把本頁輸出到一個變量中
$temp=ob_get_contents();
ob_end_clean();
//寫入文件
$fp=fopen("./index.htm",'w');
fwrite(fp,temp) or die('寫文件錯誤');
//echo"生成html完成!";

關(guān)于利用php編寫一個靜態(tài)文件生成類就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI