PHP static file generation class instance analysis_PHP tutorial

WBOY
Release: 2016-07-13 09:59:38
Original
902 people have browsed it

Instance analysis of php static file generation class

This article mainly introduces the php static file generation class, and analyzes in detail the method and method of using php to generate static files in the form of examples. For usage tips, friends in need can refer to them

The example in this article describes the PHP static file generation class. Share it with everyone for your reference.

The specific implementation method is as follows:

The code is as follows:

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.

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template