douxue7196 2019-02-09 16:34
浏览 16
已采纳

如何在用户发送值时创建PHP配置文件?

I want to know if there is anyway that php can write a config file after a user send a form (like how wordpress setup)

Since I want to make my php project setup file, so I need to write an ini file (or php), But I don't know how exactly how to.

So How am I suppose to do? because telling the user to edit the php file is too odd, as they probably won't know how to.

I have tried creating an "ini" file, but it only can read, I don't know how to write it.

  • 写回答

1条回答 默认 最新

  • dougu3290 2019-02-09 16:37
    关注

    here is a class to work with (how ever regex is not a good idea to work with!)

    class ConfigFileManager
    {
        private $configFile = null;
        private $items = array();
        function __construct($file_address)
        {
            if(file_exists($file_address))
            {
                $this->configFile = $file_address;
                $this->parse();
            }
        }
        function __get($id) { return $this->items[ $id ]; }
        function __set($id,$v) { $this->items[ $id ] = $v; }
        function parse()
        {
            if($this->configFile != null)
            {
                $fh = fopen( $this->configFile, 'r' );
                while( $l = fgets( $fh ) )
                {
                 if ( preg_match( '/^#/', $l ) == false )
                 {
                    preg_match( '/^(.*?)=(.*?)$/', $l, $found );
                    if($found)
                    $this->items[ trim($found[1]) ] = trim($found[2]);
                 }
                }
                fclose( $fh );
            }
            else
            {
              $this->file_not_exist();
            }
        }
        function save()
        {
            if($this->configFile != null)
            {
                $nf = '';
                $fh = fopen( $this->configFile, 'r' );
                while( $l = fgets( $fh ) )
                {
                    if ( preg_match( '/^#/', $l ) == false )
                    {
                        preg_match( '/^(.*?)=(.*?)$/', $l, $found );
                        $nf .= $found[1]."=".$this->items[$found[1]]."
    ";
                    }
                    else
                    {
                        $nf .= $l;
                    }
                }
                fclose( $fh );
                copy( $this->configFile, $this->configFile.'.bak' );  //backup last configs
                $fh = fopen( $this->configFile, 'w' );
                fwrite( $fh, $nf );
                fclose( $fh );
            }
            else
            {
                $this->file_not_exist();
            }
        }
        private function file_not_exist()
        {
           //throw exception that you want
            echo "File Does Not Exist";
        }
    }
    

    example of how to work with this class

        $config = new ConfigFileManager('configs/config.ini');  //opening file config.ini from configs directory  
    
        echo $config->Title;  //read  "Title" value which is equal to  "My App"  
    
        $config->Title = "Your App";  //changing value of "Title"  
    
        $config->save();  //save changes to config.ini easily
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 高价邀请复制 域天d8联网狗
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?