dongmen5867 2011-07-14 18:13
浏览 79
已采纳

在php中操作.ini文件的类

I've been playing around with a php class I found on the net called Config Magik that I use to store some info in a INI file but have recently run into some problems when using removeKey. I wanted to know if someone can point me to a similar class that would work as well or better. Or if there is a better way to go about this.

This is my function right now, after playing around with it like crazy, so it is probably very faulty.

require_once('class.ConfigMagik.php');
$config = new ConfigMagik('config.ini', true, true);

if(!empty($_GET)){
    if(!is_writeable('config.ini')){
        echo 'Could not write to config.ini';
        return false;
    }
  //if there is no section parameter, we will not do anything.
  if(!isset($_GET['section'])){ 
    echo false; return false;
  } else {
    $section_name = $_GET['section'];
    unset($_GET['section']);     //Unset section so that we can use the GET variable to manipulate the other parameters in a foreach loop.
    if (!empty($_GET)){
      foreach ($_GET as $var => $value){
            echo $var.'='.$_GET[$var].'<br />';
            //Check if said variable $var exists in the section.
        if($config->get($var, $section_name) !== NULL){
            //Set variable value.
          try{
              $config->set($var, $value, $section_name);
              echo 'Setting variable '. $var.' to '.$value.' on section '.$section_name;
          } catch(Exception $e) {
                echo 'Could not set variable '.$var;
                echo $e;
                return false;
          }
        } else {
            echo $var.' does not exist <br />';
        }
      }
    }
    try{
      $section = $config->get($section_name); //Get the entire section so that we can manipulate it.
      echo '<pre>';print_r($section);echo '</pre>';
            foreach ($section as $title=>$value){
        if(!isset($_GET[$title]) && isset($section[$title])){
            try{
            $config->removeKey($title, $section_name);
            echo '<b>'.$title.'</b>: removed <br />';
            } catch(Exception $e){
                echo $e;
          }
        }
      }
    } catch(Exception $e){
        echo $e;
    }
    $config->save();
    //echo $config->toString('HTML');
    echo true;
    return true;
  }
} else { RUN SOME HTML }

It basically saves the settings I pass on from the GET parameters and if the parameters are not there it is supposed to delete it. When I get to $config->removeKey($title, $section_name); in the last try catch statement it won't save automatically (as it should), so I tried running $config->save() and I ended up with a ini file that had section = array everywhere. Any advice will be appreciated as I've been learning PHP on the web for the last few weeks so I believe I've got a ways to go.

I have definitely isolated the problem to the $config->save() part, just don't know how to solve it.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dp20011 2011-07-14 18:38
    关注

    I have been using Zend_Config_Ini and Zend_Config_Writer_Ini in the past and was satisfied with the features. You will have extract the whole library/Zend/Config folder from Zend Framework and make Zend_Exception available though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题