The example in this article describes the PHP static file generation class. Share it with everyone for your reference.
defined('phpjb51') or die(header("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 2
if(file_exists("./index.htm"))//Check whether the static index.htm file exists
{
$time=time();
//If the file modification time is different from the current time, it will be directed to the htm file, otherwise the htm will be regenerated
if(time-filemtime("./index.htm")< 600)
{
header("location:classhtml/main.htm");
}
}
//Add ob_start() at your beginning;
ob_start();
//Homepage content 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 to file
$fp=fopen("./index.htm",'w');
fwrite(fp,temp) or die('Write file error');
//echo "Generating html completed!";
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/975894.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/975894.htmlTechArticlePHP static file generation class instance analysis This article mainly introduces the php static file generation class, which is more detailed in the form of examples. An analysis of the methods and usage techniques of using php to generate static files requires...