php静态文件生成类

这是一款可以按时间日期来生成html类,他可以生成文件,同时也支持删除文件,删除目录,具有生成静态所有功能,PHP实例代码类如下:

  1. class html
  2. {
  3. var $dir; //dir for the htmls(www.phpfensi.com/)
  4. var $rootdir; //root of html files(www.phpfensi.com/):html
  5. var $name; //html文件存放路径
  6. var $dirname; //指定的文件夹名称
  7. var $url; //获取html文件信息的来源网页地址
  8. var $time; //html文件信息填加时的时间
  9. var $dirtype; //目录存放方式:year,month,,,,
  10. var $nametype; //html文件命名方式:name
  11. function html($nametype = 'name', $dirtype = 'year', $rootdir = 'html')
  12. {
  13. $this -> setvar($nametype, $dirtype, $rootdir);
  14. }
  15. function setvar($nametype = 'name', $dirtype = 'year', $rootdir = 'html')
  16. {
  17. $this -> rootdir = $rootdir;
  18. $this -> dirtype = $dirtype;
  19. $this -> nametype = $nametype;
  20. }
  21. function createdir($dir = '')
  22. {
  23. $this -> dir = $dir?$dir:$this -> dir;
  24. if (!is_dir($this -> dir))
  25. {
  26. $temp = explode('/', $this -> dir);
  27. $cur_dir = '';
  28. for($i = 0;$i < count($temp);$i++)
  29. {
  30. $cur_dir .= $temp[$i] . '/';
  31. if (!is_dir($cur_dir))
  32. {
  33. @mkdir($cur_dir, 0777);
  34. }
  35. }
  36. }
  37. }
  38. function getdir($dirname = '', $time = 0)
  39. {
  40. $this -> time = $time?$time:$this -> time;
  41. $this -> dirname = $dirname?$dirname:$this -> dirname;
  42. switch($this -> dirtype)
  43. {
  44. case 'name':
  45. if(emptyempty($this -> dirname))
  46. $this -> dir = $this -> rootdir;
  47. else
  48. $this -> dir = $this -> rootdir . '/' . $this -> dirname;
  49. break;
  50. case 'year':
  51. $this -> dir = $this -> rootdir . '/' . date("y", $this -> time);
  52. break;
  53. case 'month':
  54. $this -> dir = $this -> rootdir . '/' . date("y-m", $this -> time);
  55. break;
  56. case 'day':
  57. $this -> dir = $this -> rootdir . '/' . date("y-m-d", $this -> time);
  58. break;
  59. }
  60. $this -> createdir();
  61. return $this -> dir;
  62. }
  63. function geturlname($url = '')
  64. {
  65. $this -> url = $url?$url:$this -> url;
  66. $filename = basename($this -> url);
  67. $filename = explode(".", $filename);
  68. return $filename[0];
  69. }
  70. function geturlquery($url = '')
  71. {
  72. $this -> url = $url?$url:$this -> url;
  73. $durl = parse_url($this -> url);
  74. $durl = explode("&", $durl[query]);
  75. foreach($durl as $surl)
  76. {
  77. $gurl = explode("=", $surl);
  78. $eurl[] = $gurl[1];
  79. }
  80. return join("_", $eurl);
  81. }
  82. function getname($url = '', $time = 0, $dirname = '')
  83. {
  84. $this -> url = $url?$url:$this -> url;
  85. $this -> dirname = $dirname?$dirname:$this -> dirname;
  86. $this -> time = $time?$time:$this -> time;
  87. $this -> getdir();
  88. switch($this -> nametype)
  89. {
  90. case 'name':
  91. $filename = $this -> geturlname() . '.htm';
  92. $this -> name = $this -> dir . '/' . $filename;
  93. break;
  94. case 'time':
  95. $this -> name = $this -> dir . '/' . $this -> time . '.htm';
  96. break;
  97. case 'query':
  98. $this -> name = $this -> dir . '/' . $this -> geturlquery() . '.htm';
  99. break;
  100. case 'namequery':
  101. $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> geturlquery() . '.htm';
  102. break;
  103. case 'nametime':
  104. $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> time . '.htm';
  105. break;
  106. }
  107. return $this -> name;
  108. }
  109. function createhtml($url = '', $time = 0, $dirname = '', $htmlname = '')
  110. {
  111. $this -> url = $url?$url:$this -> url;
  112. $this -> dirname = $dirname?$dirname:$this -> dirname;
  113. $this -> time = $time?$time:$this -> time;
  114. // 上面保证不重复地把变量赋予该类成员
  115. if(emptyempty($htmlname))
  116. $this -> getname();
  117. else
  118. $this -> name = $dirname . '/' . $htmlname; //得到name
  119. $content = file($this -> url) or die("failed to open the url " . $this -> url . " !");;
  120. // /////////////关键步---用file读取$this->url
  121. $content = join("", $content);
  122. $fp = @fopen($this -> name, "w") or die("failed to open the file " . $this -> name . " !");
  123. if(@fwrite($fp, $content))
  124. return true;
  125. else
  126. return false;
  127. fclose($fp);
  128. }
  129. // ///////////////以name为名字生成html
  130. function deletehtml($url = '', $time = 0, $dirname = '')
  131. {
  132. $this -> url = $url?$url:$this -> url;
  133. $this -> time = $time?$time:$this -> time;
  134. $this -> getname();
  135. if(@unlink($this -> name))
  136. return true;
  137. else
  138. return false;
  139. }
  140. /**
  141. * function::deletedir()
  142. * 删除目录
  143. *
  144. * @param $file 目录名(不带/)
  145. * @return
  146. */
  147. function deletedir($file)
  148. {
  149. if(file_exists($file))
  150. {
  151. if(is_dir($file))
  152. {
  153. $handle = opendir($file);
  154. while(false !== ($filename = readdir($handle)))
  155. {
  156. if($filename != "." && $filename != "..")
  157. $this -> deletedir($file . "/" . $filename);
  158. }
  159. closedir($handle);
  160. rmdir($file);
  161. return true;
  162. }else{
  163. unlink($file);
  164. }
  165. }
  166. }
  167. }