PHP generated HTML file method summary _php

Source: Internet
Author: User
Keywords PHP generating HTML
Tags smarty template
I often see people on the Internet to ask how to static the entire dynamic site, in fact, the method is very simple.

The code is as follows:


<?php
Add Ob_start () at the beginning of your service;
Ob_start ();
Here's your code.
Add Ob_end_clean () at the end and output this page to a variable
$temp = Ob_get_contents ();
Ob_end_clean ();
Write file
$fp = fopen (' filename ', ' w ');
Fwrite ($FP, $temp) or Die (' Write file error ');
?>

This is only the most basic method, is not very practical, because the site is to be updated, to periodically regenerate the HTML

Here's how I use it:

The code is as follows:


if (file_exists ("xxx.html"))
{
$time = time ();
File modification time and the current time difference of half an hour, direct to the HTML file, otherwise regenerate HTML
if ($time-filemtime ("xxx.html") < 30*60)
{
Header ("Location:xxx.html");
}
}
Add Ob_start () at the beginning of your service;
Ob_start ();
Detailed content of the page
Add Ob_end_clean () at the end and output this page to a variable
$temp = Ob_get_contents ();
Ob_end_clean ();
Write file
$fp = fopen (' xxx.html ', ' W ');
Fwrite ($FP, $temp) or Die (' Write file error ');
Re-orientation
Header ("Location:xxx.html");


The cache files used above will be overloaded when generated in large numbers, so let's introduce a more efficient approach

The following is the submission page for the input content:
File name: aa.html

The code is as follows:




Submit Page







Here is the code snippet:
File name: bb.php

The code is as follows:


<?php
Defining date Functions
function GetDateTime ()
{
$datetime =getdate ();
$strReturn = $datetime ["Year"]. " -";
$strReturn = $strReturn. $datetime ["Mon"]. " -";
$strReturn = $strReturn. $datetime ["Mday"];
return $strReturn;
}
Defining a Time function (file name)
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
Determine if the commit value is empty
$submit =$_post["Submit"];
Defining File header Information
$htmltitle =$_post["HTMLTitle"];
Define file Contents
$htmlbody =$_post["HTMLBody"];
if ($submit) {
Defining HTML file Tags
$html 1= $html 1. "";
$html 1= $html 1. "";
$html 1= $html 1. "<title>"; <br/> $html 1= $html 1. $htmltitle <br/> $html 1= $html 1."</title>";
$html 1= $html 1. " ";
$html 1= $html 1. "";
$html 1= $html 1. "";
$html 1= $html 1. "


"; $html 1= $html 1."
";
$html 1= $html 1. $htmltitle;
$html 1= $html 1. "
";
$html 1= $html 1. $htmlbody;
$html 1= $html 1. "
";
$html 1= $html 1. "";
$html 1= $html 1. "";
Determine if today's folder exists
if (!is_dir (GetDateTime ())) {
If it does not exist, establish
mkdir (GetDateTime (), 0777);
}
Write HTML file
$filedir =getdatetime ();
$filename =gettime ();
$filename = $filename. ". HTML ";
$FP =fopen ("$filedir/$filename", "w");
Fwrite ($fp, $html 1);
Fclose ($FP);
echo "";
}
?>

If the prompt file is successfully written, then you will succeed, then go back to your corresponding directory to see if there is any static HTML file generated!

Smarty Template Generation Method

The code is as follows:


<?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);
File Generation function
function Tohtmlfile_cjjer ($file _cjjer_name, $file _cjjer_content) {
if (Is_file ($file _cjjer_name)) {
@unlink ($file _cjjer_name); exist, delete it
}
$cjjer _handle = fopen ($file _cjjer_name, "w"); Create a file
if (!is_writable ($file _cjjer_name)) {//Judge Write permission
return false;
}
if (!fwrite ($cjjer _handle, $file _cjjer_content)) {
return false;
}
Fclose ($cjjer _handle); Close pointer
return $file _cjjer_name; Return file name
}
?>



There is a method fetch () in smarty that gets the template page content, and it declares the prototype as follows:

The code is as follows:


<?php
function fetch ($resource _name, $cache _id = null,
$compile _id = null, $display = False)
?>


The first parameter is the template name, the second parameter is the cached ID, the third parameter is the compilation ID, and the fourth parameter is whether the template content is displayed. Generate static page We need to use this method.

The code is as follows:


<?php
$smarty = new Smarty ();
Other template substitution syntax ...
The following sentence gets all the contents of the page, note that the last parameter is False
$content = $smarty->fetch (' template name. TPL ', NULL, NULL, FALSE);
The following writes the contents to a static file
$fp = fopen (' news.html ', ' W ');
Fwrite ($fp, $content);
Fclose ($FP);
OK, here This news.html static page is generated, you can handle your next work.
?>

All right, in combination with the above method, we generate the file almost the same principle, first read the data and then give us a defined template, and finally use the fopen function to generate an. html file

The above several methods of PHP to generate HTML static files are similar in principle, just slightly different methods, there are pros and cons, everyone according to their own project needs, free choice Bar

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.