Discuz教程网

PHP静态类

[复制链接]
authicon dly 发表于 2011-1-7 12:42:07 | 显示全部楼层 |阅读模式
  1. <?php
  2. class Shtml
  3.   {
  4.     var $Templet;
  5.     var $DataSource;
  6.     var $Dir;

  7.     var $fileName;
  8.     var $mod;
  9.     var $handle;

  10.     function Shtml($fileName="")
  11.     {
  12.       $this->fileName=$fileName;
  13.       $this->mod="wb";
  14.       $this->handle=false;

  15.       $this->Templet    = "";
  16.       $this->DataSource  = array();
  17.       $this->Dir      = "";
  18.     }
  19.    
  20.     ///  <描述>
  21.     ///  绑定数据源,参数为一数组。
  22.     ///  </描述>
  23.     function BindData($arr)
  24.     {
  25.       $this->DataSource = $arr;
  26.     }
  27.    
  28.     ///  <描述>
  29.     ///  设置文件存放路径。
  30.     ///  </描述>
  31.     function SetDir($dir)
  32.     {
  33.       $this->Dir = $dir;
  34.     }
  35.     function SetFileName($fileName)
  36.     {
  37.       return $this->fileName=$fileName;
  38.     }

  39.     function GetMod()
  40.     {
  41.       return $this->mod;
  42.     }
  43.     function SetMod($mod)
  44.     {
  45.       return $this->mod=$mod;
  46.     }
  47.     function Open()
  48.     {
  49.       if(substr($this->fileName,0,1)=="/")
  50.         $this->fileName = $_SERVER[\'DOCUMENT_ROOT\'] . $this->fileName;
  51.       if($this->handle=fopen($this->fileName, $this->mod))
  52.         return $this->handle;
  53.       else
  54.         return false;
  55.     }
  56.     function Close()
  57.     {
  58.       return fclose($this->handle);
  59.     }
  60.     function Write($content)
  61.     {
  62.       return fwrite($this->handle,$content);
  63.     }
  64.     function MkDir($pathname)
  65.     {
  66.       $currentPath="";
  67.       str_replace("\\","/",$pathname);
  68.       $pathArr = split("/",$pathname);
  69.       if($pathArr[0] == "")    //使用绝对路径
  70.       {
  71.         $currentPath = $_SERVER[\'DOCUMENT_ROOT\'];
  72.       }
  73.       else
  74.       {
  75.         $currentPath = $_SERVER[\'DOCUMENT_ROOT\'] . dirname($_SERVER[\'PHP_SELF\']);
  76.       }
  77.       for($i=0; $i<count($pathArr); $i++)
  78.       {
  79.         if($pathArr[$i]=="")
  80.           continue;
  81.         else
  82.           if(is_dir($currentPath . "/" . $pathArr[$i]))
  83.             $currentPath = $currentPath . "/" . $pathArr[$i];
  84.           else
  85.             mkdir($currentPath = $currentPath . "/" . $pathArr[$i]);
  86.       }
  87.     }

  88.     ///  <描述>
  89.     ///  生成静态文件。
  90.     ///  </描述>
  91.     function Create()
  92.     {
  93.       $tmp = $this->Templet;
  94.       foreach($this->DataSource as $key=>$value)
  95.       {
  96.         $tmp = str_replace("<FIELD_" . $key . ">", $value, $tmp);
  97.       }
  98.       $this->MkDir(dirname($this->fileName));
  99.       $this->Open();
  100.       $this->Write($tmp);
  101.       $this->Close();
  102.     }
  103.   }

  104.   function CreateShtml()
  105.   {
  106.     ob_start("callback_CteateShtml");
  107.   }
  108.   function callback_CteateShtml($buffer)
  109.   {
  110.     $page = intval(@$_REQUEST["page"]);
  111.     $shtml = new Shtml();
  112.     $shtml->SetFileName($_SERVER[\'DOCUMENT_ROOT\'] . dirname($_SERVER[\'PHP_SELF\']) . "/" . basename($_SERVER[\'PHP_SELF\'],".php") . ($page==0 ? "" : "_" . strval($page)) . ".htm");
  113.     $shtml->Templet = $buffer;
  114.     $shtml->Create();
  115.     return $buffer;
  116.   }
  117. ?>
复制代码






上一篇:身份证位数转换及验证
下一篇:php调用web service方法
authicon 蓝色天空k 发表于 2011-6-16 15:59:57 | 显示全部楼层
我回不回呢 考虑再三 还是不回了吧 ^_^
authicon cutiemimi 发表于 2011-6-18 14:59:59 | 显示全部楼层
支持楼主,顶一下
authicon Cute宝贝儿 发表于 2011-6-23 08:00:42 | 显示全部楼层
万分感谢楼主
authicon rin421 发表于 2011-6-24 11:00:04 | 显示全部楼层
这个还是不错的!
authicon icenna 发表于 2011-6-25 17:59:41 | 显示全部楼层
看一下啊,嘻嘻
authicon YY大兔子 发表于 2011-6-26 12:59:49 | 显示全部楼层
看看  好像不错
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 08:01

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表