12
2

PHPIni解析类 Beta – PHP修改删除增加配置文件

Author Jessica     Category 本站原创     Tags ,

本类具有以下优点:

1.实现了与win编程下几乎相同的ini文件操作

2.安全性高,与平时使用的php的数组存取方式一样。不会被下载。

3.以前的config.php都得手工手改。本类实现了对ini文件的读取修改删除等操作。

4.可以使用Section或非Section方式。Section就是对每个属性设计一个块。具体可以参看后面的例子。

5.程序注释清晰易懂,你可以任意修改,例子的说明非常详细。

注:这是个测试版本,可能存在BUG,欢迎大家测试寻找错误。有问题可以留言。

下面是例子和程序,最下面有打包下载。

使用例子:

  1. <?php  
  2. /**  
  3.  * PHPIni解析类详细例子  
  4.  */ 
  5. require_once('PHPIni.class.php');  
  6. //------------基础用法(不带Section)--------------  
  7. $ini = new PHPIni('config1.ini.php', false);  //文件必须为PHP结尾.保证安全性 如果文件不存在就创建  
  8. //以非Section方式打开.也就是忽略Section的存在  
  9. $ini->setKey('key1''value1');  //设置key  
  10. $ini->setKey('key2''value2');  
  11. $ini->saveIniFile();  //设置完要保存ini文件.这步很重要  
  12. print_r($ini->getIniArr());  //打印出解析ini文件得到的数组  
  13. //这时候你可以打开config1.ini.php看下设置是什么样的,然后执行下面的语句  
  14. $ini->setKey('key1''我是可以key1');  //更改key1的值  
  15. $ini->delKey('key2');   //删除key2  
  16. $ini->saveIniFile();  //保存文件  
  17. print_r($ini->getIniArr());  //打印出解析ini文件得到的数组  
  18. unset($ini);  
  19. //------------end 基础用法(不带Section)--------------  
  20.  
  21. //------------中级用法(带Section)------------------  
  22. $ini = new PHPIni('config2.ini.php');  //使用默认方式(Section方式)打开文件  
  23. //注:在Section模式下.每个键值都必须依附于一个Section.  
  24. $ini->addSection('section1');  //如果一个Section都没有就必须设置一个  
  25. $ini->setKey('key1''value1''section1');  //为Section1设置一个值  
  26. $ini->addSection('section2'array('key1' => 'value1'));  //直接为Section设置值 和上面的效果相同  
  27. $ini->setKey('key2''value2');  //Section为空.就在最后一个Section后增加值.当前就是在Section2后加入  
  28. $ini->saveIniFile();  //保存文件  
  29. print_r($ini->getIniArr());  //打印出解析ini文件得到的数组  
  30. $ini->delSection('section1');  //删除Section 注意下面的所有键值都会被删除  
  31. $ini->saveIniFile();  //保存文件  
  32. print_r($ini->getIniArr());  //打印出解析ini文件得到的数组  
  33. unset($ini);  
  34. //------------end  中级用法(带Section)------------------  
  35.  
  36. //------------高级用法(通过数组设置ini)------------------  
  37. $ini = new PHPIni('config3.ini.php');  
  38. $iniArr = array(  
  39.     'section1' => array(  
  40.         'key1' => 'value1',  
  41.         'key2' => 'value2',  
  42.     ),  
  43.     'section2' => array(  
  44.         'key1' => 'value1',  
  45.         'key2' => 'value2',  
  46.     ),  
  47. );  
  48. $ini->setIniArr($iniArr);  
  49. $ini->saveIniFile();  //把数组保存到Ini文件  
  50. //下面的方法也可以  
  51. $ini->setIniFile($iniArr); //这个方法也可以.跟上面两句话等价.  
  52. //------------end 高级用法(通过数组设置ini)------------------ 

  1. <?php  
  2. /**  
  3.  * PHPIni解析类 Beta - PHP修改删除增加配置文件  
  4.  *   
  5.  * @author Jessica  
  6.  * @link www.skiyo.cn  
  7.  * @version 1.0.0  
  8.  *  
  9.  */ 
  10. class PHPIni {  
  11.     /**  
  12.      * Ini文件的数组形式  
  13.      *  
  14.      * @var array  
  15.      */ 
  16.     private $iniArr = array();  
  17.  
  18.     /**  
  19.      * Ini文件路径  
  20.      *  
  21.      * @var string  
  22.      */ 
  23.     private $iniFile = '';  
  24.  
  25.     /**  
  26.      * 是否以Section方式打开ini文件  
  27.      *  
  28.      * @var boolean  
  29.      */ 
  30.     private $isSection = true;  
  31.  
  32.     /**  
  33.      * 构造函数  
  34.      * $iniFile表示文件路径.若不存在就自动创建  
  35.      * $isSection表示是否以Section方式打开ini文件  
  36.      *  
  37.      * @param string $iniFile  
  38.      * @param boolean $isSection  
  39.      * @access public  
  40.      */ 
  41.     public function __construct($iniFile$isSection = true) {  
  42.         $this->iniFile = $iniFile;  
  43.         $this->isSection = $isSection;  
  44.         if (emptyempty($this->iniArr)) {  
  45.             $this->parseIni();  
  46.         }  
  47.     }  
  48.  
  49.     /**  
  50.      * 解析ini文件到数组  
  51.      *   
  52.      * @access public  
  53.      * @return void  
  54.      */ 
  55.     private function parseIni() {  
  56.         if (emptyempty($this->iniFile) || !is_file($this->iniFile)) {  
  57.             if (!$this->createIniFile()) {  
  58.                 $this->throwException('无法创建Ini文件!');  
  59.             }  
  60.         }  
  61.         $this->iniArr = parse_ini_file($this->iniFile, $this->isSection);  
  62.     }  
  63.  
  64.     /**  
  65.      * 获取解析到的数组  
  66.      *  
  67.      * @return array  
  68.      * @access public  
  69.      */ 
  70.     public function getIniArr() {  
  71.         return $this->iniArr;  
  72.     }  
  73.  
  74.     /**  
  75.      * 创建Ini文件  
  76.      *  
  77.      * @return boolean  
  78.      * @access private  
  79.      */ 
  80.     private function createIniFile() {  
  81.         if (touch($this->iniFile)) {  
  82.             return true;  
  83.         } else {  
  84.             return false;  
  85.         }  
  86.     }  
  87.  
  88.     /**  
  89.      * 添加或者修改一个config  
  90.      * 如果存在就更改以前的设置.如果不存在就添加ini设置  
  91.      * 如果$section为空就在最后添加设置,如果不为空就在指定的$section添加设置  
  92.      *  
  93.      * @param string $key  
  94.      * @param mixed $value  
  95.      * @param string $section  
  96.      * @access public  
  97.      * @return void  
  98.      */ 
  99.     public function setKey($key$value$section = '') {  
  100.         //如果$section为空就在最后添加设置  
  101.         if (emptyempty($section)) {  
  102.             if ($this->isSection) {  
  103.                 //将数组指针移动到最后  
  104.                 end($this->iniArr);  
  105.                 //在数组的最后插入元素  
  106.                 if (emptyempty($this->iniArr[key($this->iniArr)])) {  
  107.                     $this->throwException('没有任何Section,请先添加Section');  
  108.                 }  
  109.                 $this->iniArr[key($this->iniArr)][$key] = $value;  
  110.             } else {  
  111.                 $this->iniArr[$key] = $value;  
  112.             }  
  113.         } else {  
  114.             //$section就按照$section添加设置  
  115.             if ($this->isSection) {  
  116.                 $this->iniArr[$section][$key] = $value;  
  117.             } else {  
  118.                 $this->iniArr[$key] = $value;  
  119.             }  
  120.         }  
  121.     }  
  122.  
  123.     /**  
  124.      * 增加一个Section,其内容为数组  
  125.      *  
  126.      * @param string $section  
  127.      * @param array $value  
  128.      */ 
  129.     public function addSection($section$value = array()) {  
  130.         if (!$this->isSection) {  
  131.             $this->throwException('无法在非Section模式下增加Section!');  
  132.         } else {  
  133.             if (array_key_exists($section$this->iniArr)) {  
  134.                 $this->throwException('无法增加Section,已经存在相同的Section!');  
  135.             } else {  
  136.                 if (is_array($value)) {  
  137.                     $this->iniArr[$section] = $value;  
  138.                 } else {  
  139.                     $this->throwException('Section的值必须是数组!');  
  140.                 }  
  141.             }  
  142.         }  
  143.     }  
  144.  
  145.     /**  
  146.      * 手工设置ini的参数.  
  147.      *  
  148.      * @param array $iniArr  
  149.      * @access public  
  150.      */ 
  151.     public function setIniArr($iniArr) {  
  152.         $this->iniArr = $iniArr;  
  153.     }  
  154.  
  155.     /**  
  156.      * 删除一个Section  
  157.      *  
  158.      * @param string $section  
  159.      */ 
  160.     public function delSection($section) {  
  161.         if ($this->isSection) {  
  162.             if (array_key_exists($section$this->iniArr)) {  
  163.                 unset($this->iniArr[$section]);  
  164.                 return true;  
  165.             } else {  
  166.                 return false;  
  167.             }  
  168.         } else {  
  169.             return false;  
  170.         }  
  171.     }  
  172.  
  173.     /**  
  174.      * 删除一个config  
  175.      * 如果$section为空就在最后一个Section删除对应的Key  
  176.      *  
  177.      * @param string $key  
  178.      * @param string $section  
  179.      */ 
  180.     public function delKey($key$section = '') {  
  181.         if (emptyempty($section)) {  
  182.             if ($this->isSection) {  
  183.                 end($this->iniArr);  
  184.                 if (array_key_exists($key$this->iniArr[key($this->iniArr)])) {  
  185.                     unset($this->iniArr[key($this->iniArr)][$key]);  
  186.                 } else {  
  187.                     return false;  
  188.                 }  
  189.             } else {  
  190.                 if (array_key_exists($key$this->iniArr)) {  
  191.                     unset($this->iniArr[$key]);  
  192.                 } else {  
  193.                     return false;  
  194.                 }  
  195.             }  
  196.         } else {  
  197.             //$section就按照$section添加设置  
  198.             if ($this->isSection) {  
  199.                 if (array_key_exists($key$this->iniArr[$section])) {  
  200.                     unset($this->iniArr[$section][$key]);  
  201.                 } else {  
  202.                     return false;  
  203.                 }  
  204.             } else {  
  205.                 if (array_key_exists($key$this->iniArr)) {  
  206.                     unset($this->iniArr[$key]);  
  207.                 } else {  
  208.                     return false;  
  209.                 }  
  210.             }  
  211.         }  
  212.         return true;  
  213.     }  
  214.  
  215.     /**  
  216.      * 删除数组中相同的元素.(只搜索第一层键值)  
  217.      *  
  218.      * @param array $array  
  219.      * @return array  
  220.      * @access private  
  221.      */ 
  222.     private function removeSameArrKey($array) {  
  223.         foreach ($array as $key => $value) {  
  224.             $keyArr[] = $key;  
  225.         }  
  226.         $keyArr = array_unique($keyArr);  
  227.         foreach ($array as $key1 => $value) {  
  228.             foreach ($keyArr as $key2) {  
  229.                 if ($key2 == $key1) {  
  230.                     $return[$key2] = $value;  
  231.                 }  
  232.             }  
  233.         }  
  234.         return $return;  
  235.     }  
  236.  
  237.     /**  
  238.      * 根据数组保存ini文件  
  239.      * 注:如果$iniArr为空默认使用事先读到的数组  
  240.      *  
  241.      * @param array $iniArr  
  242.      * @return boolean  
  243.      * @access public  
  244.      */ 
  245.     public function setIniFile($iniArr = array()) {  
  246.         if (emptyempty($this->iniFile) || !is_file($this->iniFile)) {  
  247.             $this->throwException('没有找到ini文件!');  
  248.         } else {  
  249.             //判断参数  
  250.             if (emptyempty($iniArr)) {  
  251.                 if (emptyempty($this->iniArr)) {  
  252.                     $this->throwException('没有任何可以读取的设置!请先解析Ini');  
  253.                 } else {  
  254.                     $iniArr = $this->iniArr;  
  255.                 }  
  256.             }  
  257.  
  258.             //生成Ini  
  259.             $iniArr = $this->removeSameArrKey($iniArr);  
  260.             foreach($iniArr as $key => $item) {  
  261.                 //由于只会有一层Section.所以不必递归  
  262.                 //Section  
  263.                 if(is_array($item)) {  
  264.                     $item = array_unique($item);  
  265.                     $content .= "\n[{$key}]\n";  
  266.                     foreach ($item as $key2 => $item2) {  
  267.                         if(is_numeric($item2) || is_bool($item2)) {  
  268.                             $content .= "{$key2} = {$item2}\n";  
  269.                         } else{  
  270.                             $content .= "{$key2} = \"{$item2}\"\n";  
  271.                         }  
  272.                     }  
  273.                 } else {  
  274.                     if(is_numeric($item) || is_bool($item)) {  
  275.                         $content .= "{$key} = {$item}\n";  
  276.                     } else {  
  277.                         $content .= "{$key} = \"{$item}\"\n";  
  278.                     }  
  279.  
  280.                 }  
  281.             }  
  282.  
  283.             //保证安全性  
  284.             $content = "<?php \n".$content."?>";  
  285.  
  286.             //写入文件  
  287.             if(!$handle = fopen($this->iniFile, 'w')) {  
  288.                 return false;  
  289.             }  
  290.  
  291.             if (flock($handle, LOCK_EX)) {  
  292.                 if(!fwrite($handle$content)) {  
  293.                     return false;  
  294.                 }  
  295.  
  296.                 flock($handle, LOCK_UN); // 释放锁定  
  297.  
  298.                 fclose($handle);  
  299.  
  300.                 return true;  
  301.  
  302.             } else {  
  303.                 $this->throwException('无法锁定ini文件'.$this->iniFile);  
  304.             }  
  305.         }  
  306.  
  307.     }  
  308.  
  309.     /**  
  310.      * 保存ini文件  
  311.      *  
  312.      * @return boolean  
  313.      * @access public  
  314.      */ 
  315.     public function saveIniFile() {  
  316.         if ($this->setIniFile()) {  
  317.             return true;  
  318.         } else {  
  319.             return false;  
  320.         }  
  321.     }  
  322.     /**  
  323.      * 抛出一异常信息  
  324.      *  
  325.      * @param string $message  
  326.      * @return void  
  327.      * @access protected  
  328.      */ 
  329.     protected function throwException($message) {  
  330.         throw new Exception($message);  
  331.     }  

点我打包下载

2 Comments to “ PHPIni解析类 Beta – PHP修改删除增加配置文件 ”

Post comment

分类目录

最近文章

近期评论

文章归档

标签

.net AJAX button Comet CSS Discuz! DIV+CSS Flash Form Google HTML编辑器 IE8 Java JavaScript jQuery JSP md5 MySQLReback OAuth Oracle PHP php-fpm PNG Punny SkiyoTabs tab TagCloud Vista Web2.0 Windows7 上传 加密 图标 本站原创 模板 模板引擎 源码 登录 短网址 石家庄 算法 编译 面向对象 魔术方法

链接表