Copy codeThe Code is as follows: <? Php
Class Shtml
{
Var $ Templet;
Var $ DataSource;
Var $ Dir;
Var $ fileName;
Var $ mod;
Var $ handle;
Function Shtml ($ fileName = "")
{
$ This-> fileName = $ fileName;
$ This-> mod = "wb ";
$ This-> handle = false;
$ This-> Templet = "";
$ This-> DataSource = array ();
$ This-> Dir = "";
}
/// <Description>
/// Bind the data source. The parameter is an array.
/// </Description>
Function BindData ($ arr)
{
$ This-> DataSource = $ arr;
}
/// <Description>
/// Set the file storage path.
/// </Description>
Function SetDir ($ dir)
{
$ This-> Dir = $ dir;
}
Function SetFileName ($ fileName)
{
Return $ this-> fileName = $ fileName;
}
Function GetMod ()
{
Return $ this-> mod;
}
Function SetMod ($ mod)
{
Return $ 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)
{
$ CurrentPath = "";
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 = $ currentPath. "/". $ pathArr [$ I];
Else
Mkdir ($ currentPath = $ currentPath. "/". $ pathArr [$ I]);
}
}
/// <Description>
/// Generate a static file.
/// </Description>
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_CteateShtml ");
}
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). ". htm ");
$ Shtml-> Templet = $ buffer;
$ Shtml-> Create ();
Return $ buffer;
}
?>