How php generates static html files

Source: Internet
Author: User
The benefits of generating static html files in php I believe anyone who has done a website knows that it can greatly improve the server load capacity. let's take a look at the principle of generating static html files in php: & amp; lt ;? Phpob_start (); include_on

The benefits of generating static html files in php I believe that anyone who has done a website knows that it can greatly improve server load capabilities. let's take a look at the principles of generating static html files in php:


  

In this example, the handler converts index.php to index.html. Note that the directory permission is required. the files to be converted cannot have ob_start () and ob_end_clean ().

The following provides a php static html file generation class:


 FileName = $ fileName; $ this-> mod = "wb"; $ this-> handle = false; $ this-> Templet = ""; $ this-> DataSource = array (); $ this-> Dir = "";} // bind the data source. the parameter is an array function BindData ($ arr) {$ this-> DataSource = $ arr;} // sets the file storage path function SetDir ($ dir) {$ this-> Dir = $ dir;} function SetFileName ($ fileName) {return $ this-> fileName = $ fileName;} function GetMod () {return $ this-> mod;} function SetMod ($ mod) {retu Rn $ this-> mod = $ mod;} function Open () {if (substr ($ this-> fileName, 0, 1) = "/") $ this-> fileName = $ _ SERVER ['document _ root']. $ this-> fileName; if ($ this-> handle = fopen ($ this-> fileName, $ this-> mod) return $ this-> handle; else return false;} function Close () {return fclose ($ this-> handle);} function Write ($ content) {return fwrite ($ this-> handle, $ content);} function MkDir ($ pathname) {$ curre NtPath = ""; str_replace ("\", "/", $ pathname); $ pathArr = split ("/", $ pathname ); if ($ pathArr [0] = "") {// use the absolute path $ currentPath = $ _ SERVER ['document _ root'];} else {$ currentPath = $ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']);} for ($ I = 0; $ I <count ($ pathArr); $ I ++) {if ($ pathArr [$ I] = "") continue; else if (is_dir ($ currentPath. "/". $ pathArr [$ I]) $ currentPath = $ currentPat H. "/". $ pathArr [$ I]; else mkdir ($ currentPath = $ currentPath. "/". $ pathArr [$ I]) ;}// generate the static file function Create () {$ tmp = $ this-> Templet; foreach ($ this-> DataSource as $ key => $ value) {$ tmp = str_replace ("<FIELD _". $ key. ">", $ value, $ tmp);} $ this-> MkDir (dirname ($ this-> fileName); $ this-> Open (); $ this-> Write ($ tmp); $ this-> Close () ;}} function CreateShtml () {ob_start ("callback_Ctea TeShtml ");} function callback_CteateShtml ($ buffer) {$ page = intval (@ $ _ REQUEST [" page "]); $ shtml = new Shtml (); $ shtml-> SetFileName ($ _ SERVER ['document _ root']. dirname ($ _ SERVER ['php _ SELF ']). "/". basename ($ _ SERVER ['php _ SELF '], ". php "). ($ page = 0? "":"_". Strval ($ page )). ". html "); $ shtml-> Templet = $ buffer; $ shtml-> Create (); return $ buffer;}?>

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.