//引入資料庫設定檔
- include( dirname(dirname(__FILE__))."\include\config.php" );
/**
- *
- * 將資料庫中的文章產生單個HTML檔案.
- * @param Date $Date
- * @param Time $Time
- * @param String $Content
- * @param String $Title
- */
- function GenerateHTML($Date,$Time,$Content,$Title,$Name){
//將日期、時間變數分解成數組
- $GetDateRow = explode("-", $Date);
- $GetTimeRow = explode(":",$Time);
//得到檔案的名字。比如:20121028210632.html
- $FileName = $GetDateRow[0].$GetDateRow[1].$GetDateRow[2].$GetTimeRow[0].$GetTimeRow[1].$GetTimeRow[2].".html";
//開啟並讀模數板內容
- $FP = fopen("tmp.html","r");
- $Str = fread($FP,filesize("tmp.html"));
//得到替換後的模板內容
- $Str = str_replace("{Title}",$Title, $Str);
- $Str = str_replace("{Content}", $Content, $Str);
- $Str = str_replace("{Name}", $Name, $Str);
- $Str = str_replace("{Date}", $Date,$Str);
- $Str = str_replace("{Time}", $Time, $Str);
//關閉檔案,減少伺服器的壓力。
- fclose($FP);
//將內容寫入HTML檔案
- $Handle = fopen($FileName,"w");
- fwrite($Handle,$Str);
- fclose($Handle);
//測試
- //echo "ok,done!";
- }
//資料庫的操作
- $querysql = "select * from article";
- $queryset = mysql_query($querysql);
//迴圈產生HTML檔案。
- while( $row = mysql_fetch_array($queryset) ){
- GenerateHTML($row['date'],$row['time'],$row['content'],$row['title'],$row['name']);
- }
- ?>
複製代碼 |