在 php 中操作 .ini 文件的类

发布于 2024-11-24 02:10:40 字数 2575 浏览 1 评论 0 原文

我一直在玩一个在网上找到的名为 Config Magik,我用来在 INI 文件中存储一些信息,但最近在使用 removeKey 时遇到了一些问题。我想知道是否有人可以向我推荐一个同样有效或更好的类似课程。或者是否有更好的方法来解决这个问题。

这是我现在的功能,在疯狂地使用它之后,所以它可能是非常错误的。

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 }

它基本上保存了我从 GET 参数传递的设置,如果参数不存在,则应该将其删除。当我到达 $config->removeKey($title, $section_name); 时在最后一个 try catch 语句中,它不会自动保存(因为它应该),所以我尝试运行 $config->save() ,最终得到一个 ini 文件,其中到处都有section = array。任何建议将不胜感激,因为过去几周我一直在网上学习 PHP,所以我相信我还有很长的路要走。

我肯定已经将问题隔离到 $config->save() 部分,只是不知道如何解决它。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖正好 2024-12-01 02:10:40

我一直在使用 Zend_Config_IniZend_Config_Writer_Ini过去并对这些功能感到满意。您将从 Zend Framework 中提取整个 library/Zend/Config 文件夹,并使 Zend_Exception 可用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文