2016-02-29 51 views
0

我使用parse_ini_file解析使用PHP的ini文件。在PHP中解析ini文件時如何寫入數組名稱?

現在我第一次上傳INI文件到我的服務器,然後打開它,並允許用戶MAK來的文件自定義更改。

現在,一旦用戶已編輯的文件我收到後的數據,並保存文件發送回服務器。但現在我沒有得到我的部分。 INIdetails,更新文件中的Dialplan,所以當我還想寫文件如何做到這一點?

這是代碼:

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

    /*Getting Our POST DATA from View*/ 
     $data = 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'), 
         'Param_1' => $this->input->post('Param_1'), 
         'Param_2' => $this->input->post('Param_2') 
        ); 


     /*Creating New file with the name of customer loggedin*/ 


     $name = $this->session->userdata('username'); 
     $ext = $this->session->userdata('extension');  
     $custom_file = fopen('uploads/'.$name.'_'.$ext.'.ini', 'w'); 




     fwrite($custom_file, "[INIDetails]\n"); 

     foreach ($data as $key => $value) 
     { 
      fwrite($custom_file, " $key = $value\n"); 
     } 



     fclose($custom_file); 

     /*Setting path to New CUSTOM file with customer name as prefix*/ 
     $file = $path.$custom_file; 


     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); 
      } 

     } 

     /*Creates ini file, dumps array to string and creates .INI file*/ 
     write_php_ini($data,$file); 
+0

?這是沒有意義的。 'parse_ini_file'的目的是讓你從代碼中讀取ini文件中的結構化數據。而不是相反。 – Sherif

+0

下面的函數寫ini文件] – Rajan

+0

我不會混亂的功能做了什麼。我的問題是***爲什麼你使用'parse_ini_file'來寫入ini文件?***。這不是'parse_ini_file'的用處。它用於**從** ini文件中**讀取**值到** your_code_中,而不是用於**將值寫入ini文件。 – Sherif

回答

1

從以前的代碼的罪魁禍首是你的陣列格式不正確,應該是數組的數組實現你想要什麼。

試試下面的代碼:

// First read the ini file that the user was editing 
// Your idea to read the existing ini file is good, since it will generate you the structured array 
$previous_data = parse_ini_file($path . $filename, true); 

// Overwrite/edit the previous_data using user's post data 
$previous_data['INIDetails']['SipUserName'] = $this->input->post('SipUserName'); 
$previous_data['INIDetails']['Password'] = $this->input->post('Password'); 
$previous_data['INIDetails']['Domain']  = $this->input->post('Domain'); 
$previous_data['INIDetails']['Proxy']  = $this->input->post('Proxy'); 
$previous_data['INIDetails']['Port']  = $this->input->post('Port'); 
$previous_data['INIDetails']['SipAuthName'] = $this->input->post('SipAuthName'); 
$previous_data['INIDetails']['DisplayName'] = $this->input->post('DisplayName'); 
$previous_data['INIDetails']['ServerMode'] = $this->input->post('ServerMode'); 
$previous_data['INIDetails']['UCServer'] = $this->input->post('UCServer'); 
$previous_data['INIDetails']['UCUserName'] = $this->input->post('UCUserName'); 
$previous_data['INIDetails']['UCPassword'] = $this->input->post('UCPassword'); 

$previous_data['DialPlan']['DP_Exception'] = $this->input->post('DP_Exception'); 
$previous_data['DialPlan']['DP_Rule1']  = $this->input->post('DP_Rule1'); 
$previous_data['DialPlan']['DP_Rule2']  = $this->input->post('DP_Rule2'); 

$previous_data['DialPlan']['OperationMode'] = $this->input->post('OperationMode'); 
$previous_data['DialPlan']['MutePkey']  = $this->input->post('MutePkey'); 
$previous_data['DialPlan']['Codec']   = $this->input->post('Codec'); 
$previous_data['DialPlan']['PTime']   = $this->input->post('PTime'); 
$previous_data['DialPlan']['AudioMode']  = $this->input->post('AudioMode'); 
$previous_data['DialPlan']['SoftwareAEC']  = $this->input->post('SoftwareAEC'); 
$previous_data['DialPlan']['EchoTailLength'] = $this->input->post('EchoTailLength'); 
$previous_data['DialPlan']['PlaybackBuffer'] = $this->input->post('PlaybackBuffer'); 
$previous_data['DialPlan']['CaptureBuffer'] = $this->input->post('CaptureBuffer'); 
$previous_data['DialPlan']['JBPrefetchDelay'] = $this->input->post('JBPrefetchDelay'); 
$previous_data['DialPlan']['JBMaxDelay']  = $this->input->post('JBMaxDelay'); 
$previous_data['DialPlan']['SipToS']   = $this->input->post('SipToS'); 
$previous_data['DialPlan']['RTPToS']   = $this->input->post('RTPToS'); 
$previous_data['DialPlan']['LogLevel']  = $this->input->post('LogLevel'); 

// Set Name of New file with the name of customer logged in 
$name  = $this->session->userdata('username'); 
$ext   = $this->session->userdata('extension'); 
$custom_file = "$name_$ext.ini"; 
$new_filename  = $path . $custom_file; 

// Write the INI file 
write_php_ini($data, $new_filename); 

function write_php_ini($array, $new_filename) 
{ 
    $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($new_filename, implode("\r\n", $res)); 
} 

function safefilerewrite($new_filename, $dataToSave) 
{ 
    if ($fp = fopen($new_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); 
    } 
} 

從以前的代碼中,有一堆我刪除不得體的代碼。此外,像法命名,變量命名等

太多的不一致。如果你的函數被命名爲駱駝通過你的代碼,然後裝箱它必須被命名爲駱駝套管。如果你的變量帶有下劃線,那麼通過你的代碼,它們必須有兩個或更多的措詞變量的下劃線。

我沒有編輯代碼的命名約定,這樣你就不會被混淆,但我建議要通過你的項目一致的命名約定。

更新:根據您的回答

,好像你改變你的整個代碼。我想提供使用嵌套的foreach和引用傳遞的是節省幾行的另一種方式:

$this->data['params'] = $this->parameter_m->get(); 

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

foreach ($this->data['parameters'] as $key_header => &$value_header) { 
    foreach ($value_header as $key_item => &$value_item) { 
     $value_item = $this->input->post($key_item); 
    } 
} 

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

$this->load->library('ini'); 

$file = $path . $filename; 

$ini = new INI($file); 
$ini->read($file); 
$ini->write($file, $this->data['parameters']); 
+0

是的,我已經改變了代碼,因爲我已經檢查了多行代碼的每個POST值,所以 – Rajan

+0

你能幫我這個:http://stackoverflow.com/questions/36052603/how-to-create-a-file基於數據庫值 – Rajan

+0

請幫我這個:http://stackoverflow.com/questions/36591037/write-database-values-to-a-ini-file-using-php-codeigniter – Rajan

1

最後我得到一個答案:

我會通過我從POST得到循環和意志獲取每個陣列的密鑰。然後,我將你爲什麼要使用`parse_ini_file`寫入ini文件給最終我寫方法

$this->data['params'] = $this->parameter_m->get(); 

     /*Getting the parameters to display on view*/ 

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




     while (current($this->data['parameters'])) 
     { 
      $param_set = current($this->data['parameters']); 
      $param_type = key($this->data['parameters']); 

       foreach ($param_set as $key =>$value) 
       { 

        $this->data['parameters'][$param_type][$key] = $this->input->post($key); 

       } 

       next($this->data['parameters']); 
     } 




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

     $this->load->library('ini'); 

     $file = $path.$filename; 

     $ini = new INI($file); 
     $ini->read($file); 
     $ini->write($file, $this->data['parameters']); 
+0

似乎是這樣的是完全不同於你的問題的代碼,但我更新了我的答案只是incase你想要另一種方式來實現這一點:)祝你好運隊友 – Ceeee

+1

@Ceeee你的代碼是正確的,但我只是不想爲每個POST值我寫得到所以我只是使用循環,非常感謝您的幫助 – Rajan

+0

無後顧之憂。總是在這裏幫助:)現在您的代碼比以前更好。你是對動態循環而不是逐個編輯它們:) – Ceeee