php静态文件生成类

  1. class include_createstatic
  2. {
  3. private $htmlpath = '';
  4. private $path = '';
  5. public $monthpath = '';
  6. private $listpath = '';
  7. private $content = '';
  8. private $filename = '';
  9. private $extname = '.html';
  10. public function createhtml($type,$desname,$content)
  11. {
  12. $this->htmlpath = getappinf('htmlpath');
  13. if (!file_exists($this->htmlpath))
  14. {
  15. @mkdir($this->htmlpath);
  16. }
  17. $this->path = $this->htmlpath.$this->monthpath.'/';
  18. if (!file_exists($this->path))
  19. {
  20. @mkdir($this->path);
  21. }
  22. $this->listpath = $this->htmlpath.'list/';
  23. if (!file_exists($this->listpath))
  24. {
  25. @mkdir($this->listpath);
  26. }
  27. switch ($type)
  28. {
  29. case 'index':
  30. $this->filename = $desname;
  31. break;
  32. case 'list':
  33. $this->filename = $this->listpath.$desname;
  34. break;
  35. case 'view':
  36. $this->filename = $this->path.$desname;
  37. break;
  38. }
  39. $this->filename .= $this->extname;
  40. $this->content = $content;
  41. }
  42. public function write()
  43. {
  44. $fp=fopen($this->filename,'wb');
  45. if (!is_writable($this->filename))
  46. {
  47. return false;
  48. }
  49. if (!fwrite($fp,$this->content))
  50. {
  51. return false;
  52. }
  53. fclose($fp);
  54. return $this->filename;
  55. }
  56. }
  57. //方法二
  58. if(file_exists("./index.htm"))//看静态index.htm文件是否存在
  59. {
  60. $time=time();
  61. //文件修改时间和现在时间相差?的话,直接导向htm文件,否则重新生成htm
  62. if(time-filemtime("./index.htm")< 600)
  63. {
  64. header("location:classhtml/main.htm");
  65. } //开源代码phpfensi.com
  66. }
  67. //在你的开始处加入ob_start();
  68. ob_start();
  69. //首页内容,就是你的动态部分了
  70. //在结尾加入ob_end_clean(),并把本页输出到一个变量中
  71. $temp=ob_get_contents();
  72. ob_end_clean();
  73. //写入文件
  74. $fp=fopen("./index.htm",'w');
  75. fwrite(fp,temp) or die('写文件错误');
  76. //echo"生成html完成!";