php生成靜態(tài)文件的多種方法分享
更新時(shí)間:2012年07月17日 09:51:28 作者:
最近需要將自己寫的php文件生成靜態(tài),經(jīng)過查找,發(fā)現(xiàn)有多種方法,不同需求可以選擇不同的方法
第一種:將php動(dòng)態(tài)頁面內(nèi)容生成靜態(tài)
ob_start();#開啟服務(wù)器緩存
include_once 'Index.php';
$ctx=ob_get_contents();#獲取緩存
ob_end_clean();#清空緩存
$fh=fopen("index.html","w+");
fwrite($fh,$ctx);#寫入html,生成html
fclose($fh);
/*
1、Flush:刷新緩沖區(qū)的內(nèi)容,輸出。
函數(shù)格式:flush()
說明:這個(gè)函數(shù)經(jīng)常使用,效率很高。
2、ob_start :打開輸出緩沖區(qū)
函數(shù)格式:void ob_start(void)
說明:當(dāng)緩沖區(qū)激活時(shí),所有來自PHP程序的非文件頭信息均不會(huì)發(fā)送,而是保存在內(nèi)部緩沖區(qū)。為了輸出緩沖區(qū)的內(nèi)容,可以使用ob_end_flush()或flush()輸出緩沖區(qū)的內(nèi)容。
3 、ob_get_contents :返回內(nèi)部緩沖區(qū)的內(nèi)容。
使用
函數(shù)格式:string ob_get_contents(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的內(nèi)容,如果輸出緩沖區(qū)沒有激活,則返回 FALSE 。
4、ob_get_length:返回內(nèi)部緩沖區(qū)的長(zhǎng)度。
使用方法:int ob_get_length(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的長(zhǎng)度;和ob_get_contents一樣,如果輸出緩沖區(qū)沒有激活。則返回 FALSE。
5、ob_end_flush :發(fā)送內(nèi)部緩沖區(qū)的內(nèi)容到瀏覽器,并且關(guān)閉輸出緩沖區(qū)。
使用方法:void ob_end_flush(void)
說明:這個(gè)函數(shù)發(fā)送輸出緩沖區(qū)的內(nèi)容(如果有的話)。
6、ob_end_clean:刪除內(nèi)部緩沖區(qū)的內(nèi)容,并且關(guān)閉內(nèi)部緩沖區(qū)
使用方法:void ob_end_clean(void)
說明:這個(gè)函數(shù)不會(huì)輸出內(nèi)部緩沖區(qū)的內(nèi)容而是把它刪除!
7、ob_implicit_flush:打開或關(guān)閉絕對(duì)刷新
使用方法:void ob_implicit_flush ([int flag])
*/
第二種:
php 靜態(tài)文件生成類(自家用)
<?php
class CreateHtml
{
function mkdir( $prefix= 'article' )
{
$y = date('Y');
$m = date('m');
$d = date('d');
$p=DIRECTORY_SEPARATOR;
$filePath='article'.$p.$y.$p.$m.$p.$d;
$a=explode($p,$filePath);
foreach ( $a as $dir)
{
$path.=$dir.$p;
if(!is_dir($path))
{
//echo '沒有這個(gè)目錄'.$path;
mkdir($path,0755);
}
}
return $filePath.$p;
}
function start()
{
ob_start();
}
function end()
{
$info = ob_get_contents();
$fileId = '12345';
$postfix = '.html';
$path = $this->mkdir($prefix= 'article');
$fileName = time().'_'.$fileId.$postfix;
$file=fopen($path.$fileName,'w+');
fwrite($file,$info);
fclose($file);
ob_end_flush();
}
}
?>
<?php
$s=new CreateHtml();
$s->start();
?>
<html>
<body>
asdfasdfasdfasdfasdfasdfasdfasdfasdf<br>
adfasdfasdf<br>
</body>>
</html>
<?php
$s->end();
?>
復(fù)制代碼 代碼如下:
ob_start();#開啟服務(wù)器緩存
include_once 'Index.php';
$ctx=ob_get_contents();#獲取緩存
ob_end_clean();#清空緩存
$fh=fopen("index.html","w+");
fwrite($fh,$ctx);#寫入html,生成html
fclose($fh);
/*
1、Flush:刷新緩沖區(qū)的內(nèi)容,輸出。
函數(shù)格式:flush()
說明:這個(gè)函數(shù)經(jīng)常使用,效率很高。
2、ob_start :打開輸出緩沖區(qū)
函數(shù)格式:void ob_start(void)
說明:當(dāng)緩沖區(qū)激活時(shí),所有來自PHP程序的非文件頭信息均不會(huì)發(fā)送,而是保存在內(nèi)部緩沖區(qū)。為了輸出緩沖區(qū)的內(nèi)容,可以使用ob_end_flush()或flush()輸出緩沖區(qū)的內(nèi)容。
3 、ob_get_contents :返回內(nèi)部緩沖區(qū)的內(nèi)容。
使用
函數(shù)格式:string ob_get_contents(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的內(nèi)容,如果輸出緩沖區(qū)沒有激活,則返回 FALSE 。
4、ob_get_length:返回內(nèi)部緩沖區(qū)的長(zhǎng)度。
使用方法:int ob_get_length(void)
說明:這個(gè)函數(shù)會(huì)返回當(dāng)前緩沖區(qū)中的長(zhǎng)度;和ob_get_contents一樣,如果輸出緩沖區(qū)沒有激活。則返回 FALSE。
5、ob_end_flush :發(fā)送內(nèi)部緩沖區(qū)的內(nèi)容到瀏覽器,并且關(guān)閉輸出緩沖區(qū)。
使用方法:void ob_end_flush(void)
說明:這個(gè)函數(shù)發(fā)送輸出緩沖區(qū)的內(nèi)容(如果有的話)。
6、ob_end_clean:刪除內(nèi)部緩沖區(qū)的內(nèi)容,并且關(guān)閉內(nèi)部緩沖區(qū)
使用方法:void ob_end_clean(void)
說明:這個(gè)函數(shù)不會(huì)輸出內(nèi)部緩沖區(qū)的內(nèi)容而是把它刪除!
7、ob_implicit_flush:打開或關(guān)閉絕對(duì)刷新
使用方法:void ob_implicit_flush ([int flag])
*/
第二種:
php 靜態(tài)文件生成類(自家用)
復(fù)制代碼 代碼如下:
<?php
class CreateHtml
{
function mkdir( $prefix= 'article' )
{
$y = date('Y');
$m = date('m');
$d = date('d');
$p=DIRECTORY_SEPARATOR;
$filePath='article'.$p.$y.$p.$m.$p.$d;
$a=explode($p,$filePath);
foreach ( $a as $dir)
{
$path.=$dir.$p;
if(!is_dir($path))
{
//echo '沒有這個(gè)目錄'.$path;
mkdir($path,0755);
}
}
return $filePath.$p;
}
function start()
{
ob_start();
}
function end()
{
$info = ob_get_contents();
$fileId = '12345';
$postfix = '.html';
$path = $this->mkdir($prefix= 'article');
$fileName = time().'_'.$fileId.$postfix;
$file=fopen($path.$fileName,'w+');
fwrite($file,$info);
fclose($file);
ob_end_flush();
}
}
?>
<?php
$s=new CreateHtml();
$s->start();
?>
<html>
<body>
asdfasdfasdfasdfasdfasdfasdfasdfasdf<br>
adfasdfasdf<br>
</body>>
</html>
<?php
$s->end();
?>
您可能感興趣的文章:
相關(guān)文章
POSIX 風(fēng)格和兼容 Perl 風(fēng)格兩種正則表達(dá)式主要函數(shù)的類比(preg_match, preg_replace,
POSIX 風(fēng)格和兼容 Perl 風(fēng)格兩種正則表達(dá)式主要函數(shù)的類比(preg_match, preg_replace, ereg, ereg_replace) ,需要的朋友可以參考下。2010-10-10淺談php中fopen不能創(chuàng)建中文文件名文件的問題
下面小編就為大家?guī)硪黄獪\談php中fopen不能創(chuàng)建中文文件名文件的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02php基礎(chǔ)知識(shí):函數(shù)基礎(chǔ)知識(shí)
php基礎(chǔ)知識(shí):函數(shù)基礎(chǔ)知識(shí)...2006-12-12PHP實(shí)現(xiàn)數(shù)組的笛卡爾積運(yùn)算示例
這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)組的笛卡爾積運(yùn)算,結(jié)合實(shí)例形式分析了php數(shù)組的笛卡爾積運(yùn)算相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2017-12-12php discuz 主題表和回帖表的設(shè)計(jì)
看看網(wǎng)上優(yōu)秀程序的源碼,分析優(yōu)缺點(diǎn),快速提高我們的水平。2009-03-03做了CDN獲取用戶真實(shí)IP的函數(shù)代碼(PHP與Asp設(shè)置方式)
asp取真實(shí)IP的代碼,搭環(huán)境測(cè)試無代理、一級(jí)或多級(jí)代理的情況,可以正常獲取2013-04-04