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;}?>