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. 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.

Below isMy 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.More efficient methods

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 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); // 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:


<? 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 cache id, the third parameter is the compilation id, and the fourth parameter is whether to display the template content. this method is used to generate static pages.

The code is as follows:


<? Php
$ Smarty = new Smarty ();
// Other template replacement syntax...
// The following sentence retrieves all the content on the page. Note that the last parameter is false.
$ Content = $ smarty-> fetch ('Template name. tpl', null, null, false );
// Write the content below to a static file
$ Fp = fopen('news.html ', 'w ');
Fwrite ($ fp, $ content );
Fclose ($ fp );
// OK. this news.html static page is generated here. you can handle your next step.
?>

Secret File

The above methods for generating html static files in php are similar in principle, but the methods are slightly different and both have advantages and disadvantages. you can choose one based on your project requirements.

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.