PHP静态类
程序员文章站
2022-12-27 08:30:54
复制代码 代码如下:
复制代码 代码如下:
<?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;
}
?>
推荐阅读
-
自定义PHP的错误报告处理方式
-
php查询出来的的数据返回成字符串
-
PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的
-
全局记录程序片段的运行时间 正确找到程序逻辑耗时多的断点_php技巧
-
从android客户端通过post方法发出汉字到Php,Php返回客户端,就出现乱码?怎么解决
-
iis php mysql php mysql Errcode: 28 终极解决方法
-
PHP程序员的技术成长规划_PHP
-
PHP模板引擎Smarty内置变量调解器用法详解 php smarty 余国荔 smarty assign
-
PHP多进程(4) :内部多进程
-
php array_splice定义和用法