欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)
復(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)文章

最新評(píng)論