《PHP實例:php生成html文件方法總結(jié)》要點:
本文介紹了PHP實例:php生成html文件方法總結(jié),希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP教程我經(jīng)常會在網(wǎng)上看到有人問怎么將整個動態(tài)的網(wǎng)站靜態(tài)化,其實實現(xiàn)的辦法很簡單.
代碼如下:
<?php
//在你的開始處加入 ob_start();
ob_start();
//以下是你的代碼
//在結(jié)尾加入 ob_end_clean(),并把本頁輸出到一個變量中
$temp = ob_get_contents();
ob_end_clean();
//寫入文件
$fp = fopen(‘文件名','w');
fwrite($fp,$temp) or die(‘寫文件錯誤');
?>
PHP教程這只是最基本的辦法,還不是很實用,因為網(wǎng)站是要更新的,要定期重新生成HTML
PHP教程下面是我用的辦法:
?
代碼如下:
if(file_exists(“xxx.html”))
{
??? $time = time();
???????? //文件修改時間和現(xiàn)在時間相差半小時一下的話,直接導(dǎo)向html文件,否則重新生成html
??? if($time - filemtime(“xxx.html”) < 30*60)
??? {
??????? header(“Location:xxx.html”);
??? }
}
//在你的開始處加入 ob_start();
ob_start();
//頁面的詳細(xì)內(nèi)容
//在結(jié)尾加入 ob_end_clean(),并把本頁輸出到一個變量中
$temp = ob_get_contents();
ob_end_clean();
//寫入文件
$fp = fopen(‘xxx.html','w');
fwrite($fp,$temp) or die(‘寫文件錯誤');
//重新導(dǎo)向
header(“Location:xxx.html”);
PHP教程上面用的緩存文件在大量生成時會出現(xiàn)負(fù)載過重,下面我們介紹一種更為高效的辦法
PHP教程以下是輸入內(nèi)容的提交頁面:
文件名:aa.html
代碼如下:
<html>
<head>
<title>提交頁面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="post" action="bb.php">
標(biāo)題:<input type="text" name="htmltitle"><br>
內(nèi)容:<textarea rows="8" cols="45" name="htmlbody"></textarea><br>
<input type="submit" name="submit" value="添加新聞">
</form>
</body>
</html>
PHP教程以下是代碼片段:
文件名:bb.php
代碼如下:
<?php
//定義日期函數(shù)
function getdatetime()
{
?$datetime=getdate();
?$strReturn=$datetime["year"]."-";
?$strReturn=$strReturn.$datetime["mon"]."-";
?$strReturn=$strReturn.$datetime["mday"];
?return $strReturn;
}
//定義時間函數(shù)(文件名)
function gettime()
{
?$times=getdate();
?$strtime=$times["year"];
?$strtime=$strtime.$times["mon"];
?$strtime=$strtime.$times["mday"];
?$strtime=$strtime.$times["minutes"];
?$strtime=$strtime.$times["seconds"];
?return $strtime;
}
?>
<?php
//判斷提交值是否為空
$submit=$_POST["submit"];
//定義文件頭部信息
$htmltitle=$_POST["htmltitle"];
//定義文件內(nèi)容
$htmlbody=$_POST["htmlbody"];
if ($submit) {
//定義html文件標(biāo)簽
$html1=$html1."<html>";
$html1=$html1."<head>";
$html1=$html1."<title>";
$html1=$html1.$htmltitle;
$html1=$html1."</title>";
$html1=$html1."<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>";
$html1=$html1."</head>";
$html1=$html1."<body>";
$html1=$html1."<table border='1' width='740' cellpadding='2' cellspacing='0' bordercolordark='#f7f7f7' bordercolorlight='#cccccc'><tr><td align='center' bgcolor='#f7f7f7' height='30'><font size='3'><b>";
$html1=$html1.$htmltitle;
$html1=$html1."</b></font></td></tr>";
$html1=$html1."<tr><td><font size='2'>";
$html1=$html1.$htmlbody;
$html1=$html1."</font></td></tr></table>";
$html1=$html1."</body>";
$html1=$html1."</html>";
//判斷今天的文件夾是否存在
if (!is_dir(getdatetime())) {
?//如果不存在就建立
?mkdir(getdatetime(),0777);
}
//寫成html文件
$filedir=getdatetime();
$filename=gettime();
$filename=$filename.".html";
$fp=fopen("$filedir/$filename","w");
fwrite($fp,$html1);
fclose($fp);
echo "<script>alert('文件寫入成功');location.href='111.php';</script>";
}
?>
PHP教程如果提示文件寫入成功,那你就成功了,然后回到你的相應(yīng)目錄里看看有沒有生成靜態(tài)的html文件!
PHP教程smarty模板生成辦法
代碼如下:
<?php
require_once("./config/config.php");
ob_start();
$id=$_GET[id];
$sql="select * from table_name where id='$id'";
$result=mysql_query($sql);
$rs=mysql_fetch_object($result);
$smarty->assign("showtitle",$rs->title);
$smarty->assign("showcontent",$rs->content);
$smarty->display("content.html");
$this_my_f= ob_get_contents();
ob_end_clean();
$filename = "$id.html";
tohtmlfile_cjjer($filename,$this_my_f);
// 文件生成函數(shù)
function tohtmlfile_cjjer($file_cjjer_name,$file_cjjer_content){
if (is_file ($file_cjjer_name)){
@unlink ($file_cjjer_name); //存在,就刪除
}
$cjjer_handle = fopen ($file_cjjer_name,"w"); //創(chuàng)建文件
if (!is_writable ($file_cjjer_name)){ //判斷寫權(quán)限
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //關(guān)閉指針
return $file_cjjer_name; //返回文件名
}
?>
?
smarty中有一個獲取模板頁內(nèi)容辦法fetch(), 它的聲明原形是這樣的:
代碼如下:
<?php
function fetch($resource_name, $cache_id = null,
? $compile_id = null, $display = false)
?>
第一個參數(shù)為模板名稱, 第二個參數(shù)為緩存的id, 第三個參數(shù)為編譯id, 第四個參數(shù)為是否顯示模板內(nèi)容. 生成靜態(tài)頁我們就需要用到這個辦法.
代碼如下:
<?php
?? $smarty = new Smarty();
? //其它模板替換語法...
?? //下面這句取得頁面中所有內(nèi)容, 注意最后一個參數(shù)為false
? $content = $smarty->fetch('模板名稱.tpl', null, null, false);
? //下面將內(nèi)容寫入至一個靜態(tài)文件
? $fp = fopen('news.html', 'w');
? fwrite($fp, $content);
? fclose($fp);
? //OK, 到這里這個news.html靜態(tài)頁就生成了, 你可以處理你下一步的工作了
?>
PHP教程好了結(jié)合上面的辦法我們生成文件幾乎原理都一樣,先把數(shù)據(jù)讀取出來然后給我們定義好的模板,最后利用fopen函數(shù)生成一個.html的文件
PHP教程以上幾種php生成html靜態(tài)文件的辦法原理上都大同小異,只是在辦法上略有不同,都有優(yōu)缺點,大家根據(jù)自己的項目需求,自由選擇吧
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP實例:php生成html文件方法總結(jié)》等實戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/13531.html