国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

php生成html文件方法總結

2020-03-22 19:43:19
字體:
來源:轉載
供稿:網友
我經常會在網上看到有人問怎么將整個動態的網站靜態化,其實實現的方法很簡單。復制代碼 代碼如下:
php
//在你的開始處加入 ob_start();
ob_start();
//以下是你的代碼
//在結尾加入 ob_end_clean(),并把本頁輸出到一個變量中
$temp = ob_get_contents();
ob_end_clean();
//寫入文件
$fp = fopen(‘文件名','w');
fwrite($fp,$temp) or die(‘寫文件錯誤');

這只是最基本的方法,還不是很實用,因為網站是要更新的,要定期重新生成HTML
下面是我用的方法:

復制代碼 代碼如下:
if(file_exists(“xxx.html”))
{
$time = time();
//文件修改時間和現在時間相差半小時一下的話,直接導向html文件,否則重新生成html
if($time - filemtime(“xxx.html”) 30*60)
{
header(“Location:xxx.html”);
}
}
//在你的開始處加入 ob_start();
ob_start();
//頁面的詳細內容
//在結尾加入 ob_end_clean(),并把本頁輸出到一個變量中
$temp = ob_get_contents();
ob_end_clean();
//寫入文件
$fp = fopen(‘xxx.html','w');
fwrite($fp,$temp) or die(‘寫文件錯誤');
//重新導向
header(“Location:xxx.html”);
上面用的緩存文件在大量生成時會出現負載過重,下面我們介紹一種更為高效的方法
以下是輸入內容的提交頁面:
文件名: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
內容: textarea rows="8" cols="45" name="htmlbody" /textarea br
input type="submit" name="submit" value="添加新聞"
/form
/body
/html
以下是代碼片段:
文件名:bb.php
復制代碼 代碼如下:
php
//定義日期函數
function getdatetime()
{
$datetime=getdate();
$strReturn=$datetime["year"]."-";
$strReturn=$strReturn.$datetime["mon"]."-";
$strReturn=$strReturn.$datetime["mday"];
return $strReturn;
}
//定義時間函數(文件名)
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"];
//定義文件內容
$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
}

如果提示文件寫入成功,那你就成功了,然后回到你的相應目錄里看看有沒有生成靜態的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);
// 文件生成函數
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"); //創建文件
if (!is_writable ($file_cjjer_name)){ //判斷寫權限
return false;
}
if (!fwrite ($cjjer_handle,$file_cjjer_content)){
return false;
}
fclose ($cjjer_handle); //關閉指針
return $file_cjjer_name; //返回文件名
}


smarty中有一個獲取模板頁內容方法fetch(), 它的聲明原形是這樣的:復制代碼 代碼如下:
php
function fetch($resource_name, $cache_id = null,
$compile_id = null, $display = false)

第一個參數為模板名稱, 第二個參數為緩存的id, 第三個參數為編譯id, 第四個參數為是否顯示模板內容. 生成靜態頁我們就需要用到這個方法.復制代碼 代碼如下:
php
$smarty = new Smarty();
//其它模板替換語法...
//下面這句取得頁面中所有內容, 注意最后一個參數為false
$content = $smarty- fetch('模板名稱.tpl', null, null, false);
//下面將內容寫入至一個靜態文件
$fp = fopen('news.html', 'w');
fwrite($fp, $content);
fclose($fp);
//OK, 到這里這個news.html靜態頁就生成了, 你可以處理你下一步的工作了

好了結合上面的方法我們生成文件幾乎原理都一樣,先把數據讀取出來然后給我們定義好的模板,最后利用fopen函數生成一個.html的文件以上幾種php生成html靜態文件的方法原理上都大同小異,只是在方法上略有不同,都有優缺點,大家根據自己的項目需求,自由選擇吧PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 渝北区| 徐闻县| 通城县| 玉山县| 镇平县| 道真| 定远县| 陕西省| 保康县| 保定市| 成武县| 来宾市| 康保县| 呼图壁县| 民乐县| 桃园县| 察隅县| 麦盖提县| 玛曲县| 高青县| 昌吉市| 成都市| 宜丰县| 巴彦淖尔市| 西峡县| 元谋县| 西宁市| 舟曲县| 广州市| 天等县| 衡阳市| 防城港市| 山阳县| 城固县| 惠安县| 西丰县| 广饶县| 梅河口市| 陵水| 汉沽区| 报价|