VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php静态文件生成类

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

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

出处:http://www.phpfensi.com/php/20140816/4324.html


相关教程