Php html file generation method summary

Source: Internet
Author: User
Tags smarty template
This article summarizes some of the methods used to generate html static files in php and collected on the Internet. It is very useful and analyzes their advantages and disadvantages. It is a very good article, we recommend it to you here.

This article summarizes some of the methods used to generate html static files in php and collected on the Internet. It is very useful and analyzes their advantages and disadvantages. It is a very good article, we recommend it to you here.

I often see people asking on the internet how to make the entire dynamic website static. In fact, the implementation method is very simple.

The Code is as follows:


<? Php
// Add ob_start () to your start ();
Ob_start ();
// The following is your code
// Add ob_end_clean () to the end and output this page to a variable.
$ Temp = ob_get_contents ();
Ob_end_clean ();
// Write a file
$ Fp = fopen ('file name', 'w ');
Fwrite ($ fp, $ temp) or die ('file writing error ');
?>

This is only the most basic method. It is not very practical, because the website needs to be updated and the HTML must be regenerated on a regular basis.

The following is my method:

The Code is as follows:


If(file_exists(“xxx.html "))
{
$ Time = time ();
// If the file modification time is half an hour different from the current time, direct it to the html file. Otherwise, generate a new html file.
If ($ time-filemtime(“xxx.html ") <30*60)
{
Header ("Location: xxx.html ");
}
}
// Add ob_start () to your start ();
Ob_start ();
// Page details
// Add ob_end_clean () to the end and output this page to a variable.
$ Temp = ob_get_contents ();
Ob_end_clean ();
// Write a file
$ Fp = fopen('xxx.html ', 'w ');
Fwrite ($ fp, $ temp) or die ('file writing error ');
// Redirect
Header ("Location: xxx.html ");


The cache files used above are overloaded when a large number of files are generated. Next we will introduce a more efficient method.

The following is the submission page of the input content:
File Name: aa.html

The Code is as follows:




Submit page







The following is a code snippet:
File Name: bb. php

The Code is as follows:


<? Php
// Define the date function
Function getdatetime ()
{
$ Datetime = getdate ();
$ StrReturn = $ datetime ["year"]. "-";
$ StrReturn = $ strReturn. $ datetime ["mon"]. "-";
$ StrReturn = $ strReturn. $ datetime ["mday"];
Return $ strReturn;
}
// Define the 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 whether the submitted value is null
$ Submit = $ _ POST ["submit"];
// Define the file header information
$ Htmltitle = $ _ POST ["htmltitle"];
// Define the File Content
$ Htmlbody = $ _ POST ["htmlbody"];
If ($ submit ){
// Define html file tags
$ Html1 = $ html1 ."";
$ Html1 = $ html1 ."";
$ Html1 = $ html1 .""; <Br/> $ html1 = $ html1. $ htmltitle; <br/> $ html1 = $ html1 ."";
$ Html1 = $ html1 ." ";
$ Html1 = $ html1 ."";
$ Html1 = $ html1 ."";
$ Html1 = $ html1 ."


";$ Html1 = $ html1 ."
";
$ Html1 = $ html1. $ htmltitle;
$ Html1 = $ html1 ."
";
$ Html1 = $ html1. $ htmlbody;
$ Html1 = $ html1 ."
";
$ Html1 = $ html1 ."";
$ Html1 = $ html1 ."";
// Determine whether the folder exists today
If (! Is_dir (getdatetime ())){
// If it does not exist, create
Mkdir (getdatetime (), 0777 );
}
// Write the html file
$ Filedir = getdatetime ();
$ Filename = gettime ();
$ Filename = $ filename. ". html ";
$ Fp = fopen ("$ filedir/$ filename", "w ");
Fwrite ($ fp, $ html1 );
Fclose ($ fp );
Echo "script" alert ('file written successfully'); location. href = '2017. php'; script ";
}
?>

If you are prompted that the file is successfully written, you will be successful and return 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. php ");
Ob_start ();
$ Id = $ _ GET [id];
$ SQL = "select * from table_name where ";
$ 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); // If yes, delete it.
}
$ Cjjer_handle = fopen ($ file_cjjer_name, "w"); // create a file
If (! Is_writable ($ file_cjjer_name) {// judge the write permission
Return false;
}
If (! Fwrite ($ cjjer_handle, $ file_cjjer_content )){
Return false;
}
Fclose ($ cjjer_handle); // close the pointer
Return $ file_cjjer_name; // return the file name
}
?>



There is a method fetch () in smarty to get the content of the template page. Its declaration is like this:

The Code is as follows:

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.