CopyCode The Code is as follows: <? PHP
Class html
{
VaR $ dir; // dir for the htmls (/)
VaR $ rootdir; // root of HTML files (without/): HTML
VaR $ name; // html file storage path
VaR $ dirname; // The specified folder name.
VaR $ URL; // The Source webpage address for retrieving HTML file information
VaR $ time; // time When HTML file information is added
VaR $ dirtype; // directory storage method: year, month ,,,,
VaR $ nametype; // name of the HTML file: Name
Function HTML ($ nametype = 'name', $ dirtype = 'Year', $ rootdir = 'html ')
{
$ This-> setvar ($ nametype, $ dirtype, $ rootdir );
}
Function setvar ($ nametype = 'name', $ dirtype = 'Year', $ rootdir = 'html ')
{
$ This-> rootdir = $ rootdir;
$ This-> dirtype = $ dirtype;
$ This-> nametype = $ nametype;
}
Function createdir ($ dir = '')
{
$ This-> dir = $ dir? $ Dir: $ this-> dir;
If (! Is_dir ($ this-> DIR ))
{
$ Temp = explode ('/', $ this-> DIR );
$ Cur_dir = '';
For ($ I = 0; $ I <count ($ temp); $ I ++)
{
$ Cur_dir. = $ temp [$ I]. '/';
If (! Is_dir ($ cur_dir ))
{
@ Mkdir ($ cur_dir, 0777 );
}
}
}
}
Function getdir ($ dirname = '', $ time = 0)
{
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
Switch ($ this-> dirtype)
{
Case 'name ':
If (empty ($ this-> dirname ))
$ This-> dir = $ this-> rootdir;
Else
$ This-> dir = $ this-> rootdir. '/'. $ this-> dirname;
Break;
Case 'Year ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y", $ this-> time );
Break;
Case 'month ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y-M", $ this-> time );
Break;
Case 'day ':
$ This-> dir = $ this-> rootdir. '/'. date ("Y-m-d", $ this-> time );
Break;
}
$ This-> createdir ();
Return $ this-> dir;
}
Function geturlname ($ url = '')
{
$ This-> url = $ URL? $ URL: $ this-> URL;
$ Filename = basename ($ this-> URL );
$ Filename = explode (".", $ filename );
Return $ filename [0];
}
Function geturlquery ($ url = '')
{
$ This-> url = $ URL? $ URL: $ this-> URL;
$ Durl = parse_url ($ this-> URL );
$ Durl = explode ("&", $ durl [query]);
Foreach ($ durl as $ Surl)
{
$ Gurl = explode ("=", $ Surl );
$ Eurl [] = $ gurl [1];
}
Return join ("_", $ eurl );
}
Function getname ($ url = '', $ time = 0, $ dirname = '')
{
$ This-> url = $ URL? $ URL: $ this-> URL;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> getdir ();
Switch ($ this-> nametype)
{
Case 'name ':
$ Filename = $ this-> geturlname().'.htm ';
$ This-> name = $ this-> dir. '/'. $ filename;
Break;
Case 'time ':
$ This-> name = $ this-> dir. '/'. $ this-> time.'.htm ';
Break;
Case 'query ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlquery().'.htm ';
Break;
Case 'namequery ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlname (). '-'. $ this-> geturlquery().'.htm ';
Break;
Case 'nametime ':
$ This-> name = $ this-> dir. '/'. $ this-> geturlname (). '-'. $ this-> time.'.htm ';
Break;
}
Return $ this-> name;
}
Function createhtml ($ url = '', $ time = 0, $ dirname ='', $ htmlname = '')
{
$ This-> url = $ URL? $ URL: $ this-> URL;
$ This-> dirname = $ dirname? $ Dirname: $ this-> dirname;
$ This-> time = $ time? $ Time: $ this-> time;
// Make sure that the variable is assigned to the class member without repeated modification.
If (empty ($ htmlname ))
$ This-> getname ();
Else
$ This-> name = $ dirname. '/'. $ htmlname; // get the name
$ Content = file ($ this-> URL) or die ("failed to open the URL". $ this-> URL ."! ");;
//// // Key step --- use file to read $ this-> URL
$ Content = join ("", $ content );
$ Fp = @ fopen ($ this-> name, "W") or die ("failed to open the file". $ this-> name ."! ");
If (@ fwrite ($ FP, $ content ))
Return true;
Else
Return false;
Fclose ($ FP );
}
/// // Generate HTML with name
Function deletehtml ($ url = '', $ time = 0, $ dirname = '')
{
$ This-> url = $ URL? $ URL: $ this-> URL;
$ This-> time = $ time? $ Time: $ this-> time;
$ This-> getname ();
If (@ unlink ($ this-> name ))
Return true;
Else
Return false;
}
/**
* Function: deletedir ()
* Delete A directory
* @ Param $ file directory name (/)
* @ Return
*/
Function deletedir ($ file)
{
If (file_exists ($ file ))
{
If (is_dir ($ file ))
{
$ Handle = opendir ($ file );
While (false! ==( $ Filename = readdir ($ handle )))
{
If ($ filename! = "." & $ Filename! = "..")
$ This-> deletedir ($ file. "/". $ filename );
}
Closedir ($ handle );
Rmdir ($ file );
Return true;
} Else {
Unlink ($ file );
}
}
}
}
?>