如何在codeigniter PHP中解析和编写.ini文件?

时间:2016-02-29 08:42:48

标签: php codeigniter

我正在Codeigniter中上传 INI 文件。

我上传一个带有一些参数的文件但是它的值是空白的,我将文件和文件路径存储在数据库中。

现在,我想让用户为该文件写一些值。

所以我打开文件,让用户为它添加一些值。

但是当我保存文件时,它并没有以我想要的方式存储。

当前结果

SipUserName = ""
SipAuthName = ""
DisplayName = ""
Password = ""
Domain = ""
Proxy = ""
Port = ""
ServerMode = ""
UCServer = 123456
UCPassword = 123456
DP_Exception = 123456
DP_Rule1 = 123456
DP_Rule2 = 123456
OperationMode = 123456
MutePkey = 123456
Codec = 123456
PTime = 123456
AudioMode = 123456
SoftwareAEC = 123456
EchoTailLength = 123456
PlaybackBuffer = 123456
CaptureBuffer = 123456
JBPrefetchDelay = 123456
JBMaxDelay = 123456
SipToS = ""
RTPToS = 123456
LogLevel = 123456

预期结果:

[INIDetails]
SipUserName = 
SipAuthName = 
DisplayName = 
Password = 
Domain = 
Proxy = 
Port = 
ServerMode = 
UCServer = 123456
UCPassword = 123456

[DialPlan]
DP_Exception = 123456
DP_Rule1 = 123456
DP_Rule2 = 123456

[Advanced]
OperationMode = 123456
MutePkey = 123456
Codec = 123456
PTime = 123456
AudioMode = 123456
SoftwareAEC = 123456
EchoTailLength = 123456
PlaybackBuffer = 123456
CaptureBuffer = 123456
JBPrefetchDelay = 123456
JBMaxDelay = 123456
SipToS = 
RTPToS = 123456
LogLevel = 123456

以下是我的代码:

public function edit_ini($id)
{

    /*The ID wen get from View's URI*/
        $path = "./uploads/"; 

        $site = $this->session->userdata('site');
        /*Set the path to open the file*/

        $this->db->select('*');

        $this->db->where('site_key',$site);

        $this->db->where('id',$id);
        /*Here the id it the ID we got in URI from View*/

        $this->db->from('base_ini');

        $query = $this->db->get();

        $result = $query->row();

        $filename= $result->base_ini_filename;

        $path= $result->file_path; 

        echo $this->db->last_query();

        /*Setting the Path and the filename from database.*/

        file_get_contents($path.$filename); 


        /*Get All The Contents from that file*/

        $this->data['params'] = $this->parameter_m->get();
        /*Getting the parameters to display on view*/

        $this->data['parameters']   = parse_ini_file($path.$filename,true);


        $insert = array(

                        'SipUserName'     => $this->input->post('SipUserName'), 
                        'SipAuthName'     => $this->input->post('SipAuthName'), 
                        'DisplayName'     => $this->input->post('DisplayName'),
                        'Password'        => $this->input->post('Password'), 
                        'Domain'          => $this->input->post('Domain'), 
                        'Proxy'           => $this->input->post('Proxy'),
                        'Port'            => $this->input->post('Port'), 
                        'ServerMode'      => $this->input->post('ServerMode'),
                        'UCServer'        => $this->input->post('UCServer'),
                        'UCPassword'      => $this->input->post('UCPassword'),
                        'DP_Exception'    => $this->input->post('DP_Exception'),
                        'DP_Rule1'        => $this->input->post('DP_Rule1'),
                        'DP_Rule2'        => $this->input->post('DP_Rule2'),
                        'OperationMode'   => $this->input->post('OperationMode'),
                        'MutePkey'        => $this->input->post('MutePkey'),
                        'Codec'           => $this->input->post('Codec'),
                        'PTime'           => $this->input->post('PTime'),
                        'AudioMode'       => $this->input->post('AudioMode'),
                        'SoftwareAEC'     => $this->input->post('SoftwareAEC'),
                        'EchoTailLength'  => $this->input->post('EchoTailLength'),
                        'PlaybackBuffer'  => $this->input->post('PlaybackBuffer'),
                        'CaptureBuffer'   => $this->input->post('CaptureBuffer'),
                        'JBPrefetchDelay' => $this->input->post('JBPrefetchDelay'),
                        'JBMaxDelay'      => $this->input->post('JBMaxDelay'),
                        'SipToS'          => $this->input->post('SipToS'),
                        'RTPToS'          => $this->input->post('RTPToS'),
                        'LogLevel'        => $this->input->post('LogLevel')





                    );


            $this->load->helper('file');


            $file = $path.$filename;


                    function write_php_ini($array, $file)
                    {
                        $res = array();
                        foreach($array as $key => $val)
                        {
                            if(is_array($val))
                            {
                                $res[] = "[$key]";
                                foreach($val as $skey => $sval) $res[] = "$skey = ".(is_numeric($sval) ? $sval : '"'.$sval.'"');
                            }
                            else $res[] = "$key = ".(is_numeric($val) ? $val : '"'.$val.'"');
                        }
                        safefilerewrite($file, implode("\r\n", $res));
                    }

                    function safefilerewrite($fileName, $dataToSave)
                    {    if ($fp = fopen($fileName, 'w'))
                        {
                            $startTime = microtime(TRUE);
                            do
                            {            $canWrite = flock($fp, LOCK_EX);
                               // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
                               if(!$canWrite) usleep(round(rand(0, 100)*1000));
                            } while ((!$canWrite)and((microtime(TRUE)-$startTime) < 5));

                            //file was locked so now we can store information
                            if ($canWrite)
                            {            fwrite($fp, $dataToSave);
                                flock($fp, LOCK_UN);
                            }
                            fclose($fp);
                        }

                    }


        /*Inserting The Data into .ini File*/
        write_php_ini($insert,$file);

        /*Back to you index page id data is submmited*/
        if(isset($_POST['submit'] ))
        {
            redirect('customer/upload_ini/index');
        }

        $this->data['subview'] = 'customer/upload/edit_ini';
        $this->load->view('customer/_layout_main', $this->data);



}

我想将这些部分打印到文件中。

2 个答案:

答案 0 :(得分:1)

这不是一个完整的解决方案,但会对您的代码进行一些更正

第一件事:请避免使用三元运算符,您的代码必须始终尽可能清晰,同时您还没有计划您的值为空/空的情况

请尝试以下

foreach($val as $skey => $sval) {
        $configPrefix = $skey . ' = ';

        if (!is_null($sval) && !empty($sval)) {
            if (is_numeric($sval)) {
                $res[] = $configPrefix . $sval;
            }
            else if (is_string($sval)) {
                $res[] = $configPrefix  . "'"   . 
                         $sval          . "'"   ;
            }
            else {
                // TODO Log/throw exception for your unhandled case ...
            }
        }
    }

此外,您还没有按部分分解 $ insert 数组,因此请使用此代码并调整代码以编写ini文件中的部分

$insert = array(
        'INIDetails' => array(
            'SipUserName'     => $this->input->post('SipUserName'), 
            'SipAuthName'     => $this->input->post('SipAuthName'), 
            'DisplayName'     => $this->input->post('DisplayName'),
            'Password'        => $this->input->post('Password'), 
            'Domain'          => $this->input->post('Domain'), 
            'Proxy'           => $this->input->post('Proxy'),
            'Port'            => $this->input->post('Port'), 
            'ServerMode'      => $this->input->post('ServerMode'),
            'UCServer'        => $this->input->post('UCServer'),
            'UCPassword'      => $this->input->post('UCPassword')
        ),  

        'DialPlan' => array(
            'DP_Exception'    => $this->input->post('DP_Exception'),
            'DP_Rule1'        => $this->input->post('DP_Rule1'),
            'DP_Rule2'        => $this->input->post('DP_Rule2')
        ),

        'Advanced' => array(
            'OperationMode'   => $this->input->post('OperationMode'),
            'MutePkey'        => $this->input->post('MutePkey'),
            'Codec'           => $this->input->post('Codec'),
            'PTime'           => $this->input->post('PTime'),
            'AudioMode'       => $this->input->post('AudioMode'),
            'SoftwareAEC'     => $this->input->post('SoftwareAEC'),
            'EchoTailLength'  => $this->input->post('EchoTailLength'),
            'PlaybackBuffer'  => $this->input->post('PlaybackBuffer'),
            'CaptureBuffer'   => $this->input->post('CaptureBuffer'),
            'JBPrefetchDelay' => $this->input->post('JBPrefetchDelay'),
            'JBMaxDelay'      => $this->input->post('JBMaxDelay'),
            'SipToS'          => $this->input->post('SipToS'),
            'RTPToS'          => $this->input->post('RTPToS'),
            'LogLevel'        => $this->input->post('LogLevel')
        )
    );

答案 1 :(得分:0)

parse_ini_file(),并处理关联数组。

样品:

$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);

你必须传递 true 作为第二个参数才能获得正确的格式。