PHP static file generation class instance analysis
This article mainly introduces the PHP static file generation class, in the case of the form of a more detailed analysis of the use of PHP to generate static files and the use of skills, the need for friends can refer to the next
The example in this paper describes the PHP static file generation class. Share to everyone for your reference.
The implementation method is as follows:
The code is as follows:
Defined (' phpjb51 ') or Die (Headers ("http/1.1 403 Not Forbidden"));
Class Include_createstatic
{
Private $htmlpath = ';
Private $path = ';
Public $monthpath = ';
Private $listpath = ';
Private $content = ';
Private $filename = ';
Private $extname = '. html ';
Public Function createhtml ($type, $desname, $content)
{
$this->htmlpath = Getappinf (' Htmlpath ');
if (!file_exists ($this->htmlpath))
{
@mkdir ($this->htmlpath);
}
$this->path = $this->htmlpath. $this->monthpath. ' /';
if (!file_exists ($this->path))
{
@mkdir ($this->path);
}
$this->listpath = $this->htmlpath. ' list/';
if (!file_exists ($this->listpath))
{
@mkdir ($this->listpath);
}
Switch ($type)
{
Case ' index ':
$this->filename = $desname;
Break
Case ' list ':
$this->filename = $this->listpath. $desname;
Break
Case ' view ':
$this->filename = $this->path. $desname;
Break
}
$this->filename. = $this->extname;
$this->content = $content;
}
Public Function Write ()
{
$FP =fopen ($this->filename, ' WB ');
if (!is_writable ($this->filename))
{
return false;
}
if (!fwrite ($fp, $this->content))
{
return false;
}
Fclose ($FP);
return $this->filename;
}
}
Method Two
if (File_exists ("./index.htm"))//See if the static index.htm file exists
{
$time =time ();
File modification time and current time difference?, direct-oriented HTM file, otherwise regenerate HTM
if (Time-filemtime ("./index.htm") < 600)
{
Header ("location:classhtml/main.htm");
}
}
Add Ob_start () at the beginning of your service;
Ob_start ();
The content of the homepage is your dynamic part.
Add Ob_end_clean () at the end and output this page to a variable
$temp =ob_get_contents ();
Ob_end_clean ();
Write file
$FP =fopen ("./index.htm", ' W ');
Fwrite (Fp,temp) or Die (' Write file error ');
echo "Generate HTML complete! ";
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/975894.html www.bkjia.com true http://www.bkjia.com/PHPjc/975894.html techarticle example Analysis of PHP static file generation class This article mainly introduces the PHP static file generation class, in the case of the form of a more detailed analysis of the use of PHP to generate static files and the use of the technique, need ...