PHP静态类
程序员文章站
2023-12-25 13:15:33
复制代码 代码如下:
复制代码 代码如下:
<?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 = "";
}
/// <描述>
/// 绑定数据源,参数为一数组。
/// </描述>
function binddata($arr)
{
$this->datasource = $arr;
}
/// <描述>
/// 设置文件存放路径。
/// </描述>
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] == "") //使用绝对路径
{
$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]);
}
}
/// <描述>
/// 生成静态文件。
/// </描述>
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;
}
?>
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 = "";
}
/// <描述>
/// 绑定数据源,参数为一数组。
/// </描述>
function binddata($arr)
{
$this->datasource = $arr;
}
/// <描述>
/// 设置文件存放路径。
/// </描述>
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] == "") //使用绝对路径
{
$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]);
}
}
/// <描述>
/// 生成静态文件。
/// </描述>
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;
}
?>