VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 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.      
  12.      
  13.     function html($nametype = 'name', $dirtype = 'year', $rootdir = 'html') 
  14.     { 
  15.         $this -> setvar($nametype, $dirtype, $rootdir); 
  16.         } 
  17.      
  18.     function setvar($nametype = 'name', $dirtype = 'year', $rootdir = 'html') 
  19.     { 
  20.         $this -> rootdir = $rootdir; 
  21.         $this -> dirtype = $dirtype; 
  22.         $this -> nametype = $nametype; 
  23.         } 
  24.      
  25.     function createdir($dir = '') 
  26.     { 
  27.         $this -> dir = $dir?$dir:$this -> dir; 
  28.          
  29.         if (!is_dir($this -> dir)) 
  30.             { 
  31.             $temp = explode('/', $this -> dir); 
  32.             $cur_dir = ''; 
  33.             for($i = 0;$i < count($temp);$i++) 
  34.             { 
  35.                 $cur_dir .= $temp[$i] . '/'; 
  36.                 if (!is_dir($cur_dir)) 
  37.                     { 
  38.                     @mkdir($cur_dir, 0777); 
  39.                     } 
  40.                 } 
  41.             } 
  42.         } 
  43.      
  44.     function getdir($dirname = '', $time = 0) 
  45.     { 
  46.         $this -> time = $time?$time:$this -> time; 
  47.         $this -> dirname = $dirname?$dirname:$this -> dirname; 
  48.          
  49.         switch($this -> dirtype) 
  50.         { 
  51.         case 'name': 
  52.             if(emptyempty($this -> dirname)) 
  53.                 $this -> dir = $this -> rootdir; 
  54.             else 
  55.                 $this -> dir = $this -> rootdir . '/' . $this -> dirname; 
  56.             break; 
  57.         case 'year': 
  58.             $this -> dir = $this -> rootdir . '/' . date("y", $this -> time); 
  59.             break; 
  60.          
  61.         case 'month': 
  62.             $this -> dir = $this -> rootdir . '/' . date("y-m", $this -> time); 
  63.             break; 
  64.          
  65.         case 'day': 
  66.             $this -> dir = $this -> rootdir . '/' . date("y-m-d", $this -> time); 
  67.             break; 
  68.             } 
  69.          
  70.         $this -> createdir(); 
  71.          
  72.         return $this -> dir; 
  73.         } 
  74.      
  75.     function geturlname($url = '') 
  76.     { 
  77.         $this -> url = $url?$url:$this -> url; 
  78.          
  79.         $filename = basename($this -> url); 
  80.         $filename = explode(".", $filename); 
  81.         return $filename[0]; 
  82.         } 
  83.      
  84.     function geturlquery($url = '') 
  85.     { 
  86.         $this -> url = $url?$url:$this -> url; 
  87.          
  88.         $durl = parse_url($this -> url); 
  89.         $durl = explode("&", $durl[query]); 
  90.         foreach($durl as $surl) 
  91.         { 
  92.             $gurl = explode("=", $surl); 
  93.             $eurl[] = $gurl[1]; 
  94.             } 
  95.         return join("_", $eurl); 
  96.         } 
  97.      
  98.     function getname($url = '', $time = 0, $dirname = '') 
  99.     { 
  100.         $this -> url = $url?$url:$this -> url; 
  101.         $this -> dirname = $dirname?$dirname:$this -> dirname; 
  102.         $this -> time = $time?$time:$this -> time; 
  103.          
  104.         $this -> getdir(); 
  105.          
  106.         switch($this -> nametype) 
  107.         { 
  108.         case 'name': 
  109.             $filename = $this -> geturlname() . '.htm'; 
  110.             $this -> name = $this -> dir . '/' . $filename; 
  111.             break; 
  112.          
  113.         case 'time': 
  114.             $this -> name = $this -> dir . '/' . $this -> time . '.htm'; 
  115.             break; 
  116.          
  117.         case 'query': 
  118.             $this -> name = $this -> dir . '/' . $this -> geturlquery() . '.htm'; 
  119.             break; 
  120.          
  121.         case 'namequery': 
  122.             $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> geturlquery() . '.htm'; 
  123.             break; 
  124.          
  125.         case 'nametime': 
  126.             $this -> name = $this -> dir . '/' . $this -> geturlname() . '-' . $this -> time . '.htm'; 
  127.             break; 
  128.              
  129.             } 
  130.         return $this -> name; 
  131.         } 
  132.      
  133.     function createhtml($url = '', $time = 0, $dirname = '', $htmlname = '') 
  134.     { 
  135.         $this -> url = $url?$url:$this -> url; 
  136.         $this -> dirname = $dirname?$dirname:$this -> dirname; 
  137.         $this -> time = $time?$time:$this -> time; 
  138.         // 上面保证不重复地把变量赋予该类成员 
  139.         if(emptyempty($htmlname)) 
  140.             $this -> getname(); 
  141.         else 
  142.             $this -> name = $dirname . '/' . $htmlname; //得到name  
  143.          
  144.          
  145.         $content = file($this -> url) or die("failed to open the url " . $this -> url . " !");; 
  146.          
  147.         // /////////////关键步---用file读取$this->url 
  148.          
  149.         $content = join("", $content); 
  150.         $fp = @fopen($this -> name, "w") or die("failed to open the file " . $this -> name . " !"); 
  151.         if(@fwrite($fp, $content)) 
  152.             return true; 
  153.         else 
  154.             return false; 
  155.         fclose($fp); 
  156.         } 
  157.     // ///////////////以name为名字生成html 
  158.     function deletehtml($url = '', $time = 0, $dirname = '') 
  159.     { 
  160.         $this -> url = $url?$url:$this -> url; 
  161.         $this -> time = $time?$time:$this -> time; 
  162.          
  163.         $this -> getname(); 
  164.          
  165.         if(@unlink($this -> name)) 
  166.             return true; 
  167.         else 
  168.             return false; 
  169.         } 
  170.      
  171.     /** 
  172.      * function::deletedir()  
  173.      * 删除目录 
  174.      *  
  175.      * @param $file 目录名(不带/) 
  176.      * @return  
  177.      */ 
  178.     function deletedir($file) 
  179.     { 
  180.         if(file_exists($file)) 
  181.             { 
  182.             if(is_dir($file)) 
  183.                 { 
  184.                 $handle = opendir($file); 
  185.                 while(false !== ($filename = readdir($handle))) 
  186.                 { 
  187.                     if($filename != "." && $filename != "..") 
  188.                         $this -> deletedir($file . "/" . $filename); 
  189.                     } 
  190.                 closedir($handle); 
  191.                 rmdir($file); 
  192.                 return true; 
  193.                 }else{ 
  194.                 unlink($file); 
  195.                 } 
  196.             } 
  197.         } 
  198.      
  199.     } 
  200.  

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


相关教程