首頁  >  文章  >  後端開發  >  php產生靜態檔案的多種方法分享

php產生靜態檔案的多種方法分享

高洛峰
高洛峰原創
2016-12-01 09:57:581072瀏覽

第一種:將php動態頁麵內容產生靜態
複製程式碼 程式碼如下: 
ob_start();#開啟伺服器快取 
include_once 'Index.php'; 
$ctx=ob_get_contents(); 取得快取記憶體取得快取;#清空快取 
$fh=fopen("index.html","w+"); 
fwrite($fh,$ctx);#寫入html,產生html 
fclose($fh); 
/* 
1 、Flush:刷新緩衝區的內容,輸出。 
函數格式:flush() 
說明:這個函數常用,效率很高。 
2、ob_start :開啟輸出緩衝區 
函數格式:void ob_start(void) 
說明:當緩衝區啟動時,所有來自PHP程式的非檔案頭資訊不會傳送,而是保存在內部緩衝區。為了輸出緩衝區的內容,可以使用ob_end_flush()或flush()輸出緩衝區的內容。 
3 、ob_get_contents :傳回內部緩衝區的內容。 
使用 
函數格式:string ob_get_contents(void) 
說明:這個函數會傳回目前緩衝區中的內容,如果輸出緩衝區沒有激活,則傳回 FALSE 。 
4、ob_get_length:傳回內部緩衝區的長度。 
使用方法:int ob_get_length(void) 
說明:這個函數會回傳目前緩衝區中的長度;就像ob_get_contents一樣,如果輸出緩衝區沒有啟動。則回傳 FALSE。 
5、ob_end_flush :將內部緩衝區的內容傳送至瀏覽器,並且關閉輸出緩衝區。 
使用方法:void ob_end_flush(void) 
說明:這個函數會傳送輸出緩衝區的內容(如果有的話)。 
6、ob_end_clean:刪除內部緩衝區的內容,並且關閉內部緩衝區 
使用方法:void ob_end_clean(void) 
說明:這個函數不會輸出內部緩衝區的內容而是刪除它!
7、ob_implicit_flush:開啟或關閉絕對刷新 
使用方法:void ob_implicit_flush ([int flag]) 
*/ 

第二種:
php 靜態檔案產生類別(自家)
class CreateHtml 

function mkdir( $prefix= 'article' ) 

$y = date('Y'); 
$m = date('m'm'); ; 
$p=DIRECTORY_SEPARATOR; 
$filePath='article'.$p.$y.$p.$m.$p.$d; 
$a=explode($p,$filePath); 
foreach
$a=explode($p,$filePath); 
foreach ( $ a as $dir) 

$path.=$dir.$p; 
if(!is_dir($path)) 

//echo '沒有這目錄'.$path 
mkdir($path,0755; ); 


return $filePath.$p; 

function start() 

ob_start(); 


ob_start(); $fileId = ' 12345'; 
$postfix = '.html'; 
$path = $this->mkdir($prefix= 'article'); 
$fileName = time().'_'.$fileId.$postfix; 
$fileName = time().'_'.$fileId.$postfix; 
$ file=fopen($path.$fileName,'w+'); 
fwrite($file,$info); 
fclose($file); 
ob_end_flush(); 


?> s=new CreateHtml(); 
$s->start(); 
?> 
 

 
asdfasdfasdfasdfasdfasdfasdfasdf ?php 
$s->end(); 
?> 
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn