php生成html文件方法總結(jié)
我經(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(‘寫文件錯誤');
?>
這只是最基本的方法,還不是很實用,因為網(wǎng)站是要更新的,要定期重新生成HTML
下面是我用的方法:
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();
//頁面的詳細內(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”);
上面用的緩存文件在大量生成時會出現(xiàn)負載過重,下面我們介紹一種更為高效的方法
以下是輸入內(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">
標題:<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>
以下是代碼片段:
文件名: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文件標簽
$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>";
}
?>
如果提示文件寫入成功,那你就成功了,然后回到你的相應(yīng)目錄里看看有沒有生成靜態(tài)的html文件!
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)頁就生成了, 你可以處理你下一步的工作了
?>
好了結(jié)合上面的方法我們生成文件幾乎原理都一樣,先把數(shù)據(jù)讀取出來然后給我們定義好的模板,最后利用fopen函數(shù)生成一個.html的文件
以上幾種php生成html靜態(tài)文件的方法原理上都大同小異,只是在方法上略有不同,都有優(yōu)缺點,大家根據(jù)自己的項目需求,自由選擇吧
相關(guān)文章
PHP中模糊查詢并關(guān)聯(lián)三個select框
這篇文章主要介紹了PHP中模糊查詢并關(guān)聯(lián)三個select框,需要的朋友可以參考下2017-06-06Laravel框架模型的創(chuàng)建及模型對數(shù)據(jù)操作示例
這篇文章主要介紹了Laravel框架模型的創(chuàng)建及模型對數(shù)據(jù)操作,結(jié)合實例形式分析了Laravel框架創(chuàng)建模型及使用模型進行數(shù)據(jù)的增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Symfony實現(xiàn)行為和模板中取得request參數(shù)的方法
這篇文章主要介紹了Symfony實現(xiàn)行為和模板中取得request參數(shù)的方法,實例分析了Symfony針對行為和方法中參數(shù)獲取的技巧,需要的朋友可以參考下2016-03-03