首頁  >  文章  >  後端開發  >  php寫的一個產生靜態頁面的類

php寫的一個產生靜態頁面的類

WBOY
WBOY原創
2016-07-25 09:05:49806瀏覽
  1. /**

  2. 功能:產生靜態頁面
  3. 整理:腳本學堂 bbs.it-home.org
  4. 日期:2013-2-16
  5. */
  6. class html
  7. {
  8. var $dir ; //dir for the htmls(without/)
  9. var $rootdir; //root of html files(without/):html
  10. var $name; //html檔案存放路徑
  11. var $dirname; //指定的資料夾名稱
  12. var $url; //取得html檔案資訊的來源網頁位址
  13. var $time; //html檔案資訊填加時的時間
  14. var $dirtype; //目錄存放方式:year,month,,,,
  15. var $nametype; //html檔案命名方式:name
  16. function html($nametype='name',$dirtype=' year',$rootdir='html')

  17. {
  18. $this->setvar($nametype,$dirtype,$rootdir);
  19. }
  20. function setvar( $nametype='name',$dirtype='year',$rootdir='html')

  21. {
  22. $this->rootdir=$rootdir;
  23. $this->dirtype=$dirtype;
  24. $this->nametype=$nametype;
  25. }
  26. function createdir($dir='')

  27. {
  28. $this->dir=$dir? $dir:$this->dir;
  29. if (!is_dir($this->dir))

  30. {
  31. $temp = explode('/',$this- >dir);
  32. $cur_dir = '';
  33. for($i=0;$i{
  34. $cur_dir .= $temp[$ i].'/';
  35. if (!is_dir($cur_dir))
  36. {
  37. @mkdir($cur_dir,0777);
  38. }
  39. }
  40. }
  41. }
  42. }
  43. function getdir($dirname='',$time=0)

  44. {
  45. $this->time=$time?$time:$this->time;
  46. $this->dirname=$dirname?$dirname:$this->dirname;
  47. switch($this->dirtype)

  48. {
  49. case 'name' :
  50. if(empty($this->dirname))
  51. $this->dir=$this->rootdir;
  52. else
  53. $this->dir=$this->rootdir.' /'.$this->dirname;
  54. break;
  55. case 'year':
  56. $this->dir=$this->rootdir.'/'.date("Y",$this- >time);
  57. break;
  58. case 'month':

  59. $this->dir=$this->rootdir.'/'.date("Y-m",$ this->time);
  60. break;
  61. case 'day':

  62. $this->dir=$this->rootdir.'/'.date("Y-m-d" ,$this->time);
  63. break;
  64. }
  65. $this->createdir();

  66. return $this-> dir;

  67. }
  68. function geturlname($url='')

  69. {
  70. $this->url=$url?$url:$this->url;
  71. $filename=basename($this->url);

  72. $filename=explode(".",$filename);
  73. return $filename[0];
  74. }
  75. function geturlquery($url='')

  76. {
  77. $this->url=$url?$url:$this->url;
  78. $durl=parse_url($this->url);

  79. $durl=explode("&",$durl[query]);
  80. foreach($durl as $surl)
  81. {
  82. $gurl=explode("=",$surl);
  83. $eurl[]=$gurl[1];
  84. }
  85. return join("_",$eurl);
  86. }
  87. function getname($url='',$time=0,$dirname='')

  88. {
  89. $this->url=$url? $url:$this->url;
  90. $this->dirname=$dirname?$dirname:$this->dirname;
  91. $this->time=$time?$time:$this->time ;
  92. $this->getdir();

  93. switch($this->nametype)

  94. {
  95. case 'name':
  96. $filename=$this->geturlname().'.htm';
  97. $this->name=$this->dir.'/'.$filename;
  98. break;
  99. case 'time':

  100. $this->name=$this->dir.'/'.$this->time.'.htm';
  101. break;
  102. case 'query':

  103. $this->name=$this->dir.'/'.$this->geturlquery().'.htm';
  104. break;
  105. case 'namequery':

  106. $this->name=$this->dir.'/'.$this->geturlname().'-'.$this->geturlquery() .'.htm';
  107. break;
  108. case 'nametime':

  109. $this->name=$this->dir.'/'.$this->geturlname ().'-'.$this->time.'.htm';
  110. break;
  111. }

  112. return $this->name;}
  113. function createhtml($url='',$time=0,$dirname='',$htmlname='')

  114. {
  115. $this-> url=$ url?$url:$this->url;
  116. $this->dirname=$dirname?$dirname:$this->dirname;
  117. $this->time=$time ?$time:$this- >time;
  118. //上面保證不重複地把戒指賦予該類成員
  119. if(empty($htmlname))
  120. $this->getname();
  121. else
  122. $this ->name=$dirname.'/'.$htmlname; //取得name
  123. $content=file($this->url) or die("無法開啟url ".$this->url." !" );;
  124. ///////////////關鍵步---用檔案讀取$this->url

  125. $content=join( "",$content);
  126. $fp=@fopen($this->name,"w") 或 die("無法開啟檔案「.$this->name。」!");
  127. if (@fwrite ($fp,$content))
  128. 回傳true;
  129. 否則
  130. 回傳false;
  131. fclose($fp);
  132. }
  133. /////// /// /////// 以name為名字產生html
  134. function deletehtml($url='',$time=0,$dirname='')
  135. {
  136. $this-> url= $url?$url:$this->url;
  137. $this->time=$time?$time:$this->time;
  138. $this ->getname ();

  139. if(@unlink($this->name))

  140. 回傳true;
  141. else
  142. 回傳false;
  143. }
  144. /**

  145. * function::deletedir()
  146. * 刪除目錄
  147. * @param $file 目錄名(不含/)
  148. * @return
  149. */
  150. 函數deletedir($file)
  151. {
  152. if(file_exists($file))
  153. {
  154. if(is_dir($file ))
  155. {
  156. $handle =opendir($file);
  157. while(false!==($filename=readdir($handle)))
  158. {
  159. if($filename! ="."&&$filename!="..")
  160. $this->deletedir($file."/".$filename);
  161. }
  162. closedir($handle);
  163. rmdir($file);
  164. 回傳true;
  165. }else{
  166. 取消連結($file);
  167. }
  168. }
  169. }
  170. }
  171. ?>
複製程式碼

您可能有興趣的文章: php產生靜態頁面的清晰方法與程式碼詳解 php 產生靜態頁面函數(php2html) 的範例 php產生靜態頁面的方法(三個函數) 細說php產生靜態檔之模板與快取 虛擬主機上定時自動產生靜態頁面的方法 php產生靜態檔案的兩種方法 php 產生靜態頁面的詳細教學 php產生靜態html檔案的原理分析 smarty 產生靜態頁面的方法 了解php產生靜態HTML檔的原理 PHP 產生靜態頁的方法 php產生靜態html檔案的透明方法



陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn