管理画面で入力したタイトルや本文の内容を、テンプレートのファイルに埋め込ませて、htmlファイルを自動生成するコードです。
管理画面で入力した内容を、テンプレートファイルの任意の場所に埋め込ませ、新しいファイルを作成しています。
管理画面の入力フォーム
<form action="../sinnkiji/makefile.php" method="POST"> ページタイトル:<br /> <input type="text" name="pagetitle" size="70" value=""/><br /> キーワード:<br /> <input type="text" name="keyword" size="100" value=""/><br /> ディスクリプション:<br /> <textarea name="dis" rows="3" cols="100"/></textarea><br /> <input type="text" name="gaurl" size="60"value="XXXXXXXXXXXXXXXXXXXXXXX"><br /> サブタイトル:<br /> <input type="text" name="sabutitle" size="50" value=" "/><br /> 本文<br /> <textarea name="honbun" rows="6" cols="100" >(<?php print date('Y年m月d日'); ?>更新)</textarea><br /> 画像下本文(画像の下にここに記入した文字が入ります)<br /> <input type="submit" value="送信" /> </form>
ファイル作成の為のテンプレート
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><%PAGETITLE></title> <meta name="Description" content="<%PAGEDIS>" /> </head> <body> <h1><%PAGETITLE></h1> <h2>記事本文</h2> <%PAGECONTENTS> </body> </html>
ファイル作成するプログラム makefile.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>テンプレ</title> </head> <body> <?php $template = "template.php"; // テンプレートファイル名 if ($_POST{"honbun"}) { // ※1 POSTデータを全て受け取りエスケープして変数に入れる foreach($_POST as $k => $v) { if(get_magic_quotes_gpc()) { $v=stripslashes($v); } // $v=htmlspecialchars($v); $array[$k]=$v; } extract($array); // 文字コードはUTF-8 $keyword = mb_convert_encoding($keyword, "UTF-8","AUTO"); $dis = mb_convert_encoding($dis, "UTF-8","AUTO"); $gaurl = mb_convert_encoding($gaurl, "UTF-8","AUTO"); $sabutitle = mb_convert_encoding($sabutitle, "UTF-8","AUTO"); $honbun = mb_convert_encoding($honbun, "UTF-8","AUTO"); $honbun1 = mb_convert_encoding($honbun1, "UTF-8","AUTO"); $pagetitle = mb_convert_encoding( htmlspecialchars($pagetitle), "UTF-8","AUTO"); // 改行を<br>タグに変換 $honbun = nl2br($honbun); $honbun1 = nl2br($honbun1); $dis = nl2br($dis); $keyword = nl2br($keyword); $sabutitle = nl2br($sabutitle); // 時間をファイル名に $filename = date("YmdHis").$_FILES['userfile']['name'] . ".html"; // ※2 メッセージ表示 if (createNewPage( $filename, $template, $pagetitle, $keyword, $dis, $sabutitle, $gaurl, $honbun, $honbun1)) { echo $filename. "を生成し、書き込みを行いました。"; } else { echo "ファイルの生成に失敗しました。"; } } else { echo "フォームから記事の内容を送信してください。"; } // ※3 ページ生成関数 createNewPage() function createNewPage( $filename, $template, $pagetitle, $keyword, $dis, $sabutitle, $gaurl, $honbun, $honbun1) { // ※4 テンプレートファイルの読み込み if ( ($contents = file_get_contents( $template)) == FALSE) { return false; } // タイトルと記事本文を挿入 $contents = str_replace( "<%PAGETITLE>", $pagetitle, $contents); $contents = str_replace( "<%PAGECONTENTS>", $honbun, $contents); $contents = str_replace( "<%PAGECONTENTS1>", $honbun1, $contents); $contents = str_replace( "<%PAGEKEYWORD>", $keyword, $contents); $contents = str_replace( "<%PAGEDIS>", $dis, $contents); $contents = str_replace( "<%PAGESABUTITLE>", $sabutitle, $contents); $contents = str_replace( "<%PAGEGAURL>", $gaurl, $contents); // ※5 ファイル生成&書き込み if ( ($handle = fopen( $filename, 'w')) == FALSE) { return false; } fwrite( $handle, $contents); fclose( $handle ); return true; } ?> <br /> <a><a href="XXXXXXXXXXXXXXXXX">管理画面に戻る</a> </body> </html>