0

Hi I want to write a ini file into php. first i upload a ini file to my server then i edit it, i want to make some changes in parameters then want save that file back to uploads.

I used put_file_contents and fwrite but didn't get required result.

this is my code:

$data = array('upload_data' => $this->upload->data());
foreach ($data as  $info) 
{
    $filepath = $info['file_path'];
    $filename= $info['file_name'];
}
$this->data['parameters']   = parse_ini_file($filepath.$filename);
$insert = array(
                'OTAID' => $this->input->post('OTAID'), 
                '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'), 
                'ServerMode' => $this->input->post('ServerMode')  
                );
$this->load->helper('file');
$file =fopen($filepath.$filename,'w');
fwrite($file,implode('', $insert));
$this->data['subview'] = 'customer/upload/upload_success';
$this->load->view('customer/_layout_main', $this->data);
Milap
  • 6,915
  • 8
  • 26
  • 46
Rajan
  • 2,427
  • 10
  • 51
  • 111

2 Answers2

1

As I told you in the last question you will need to use a helper function to turn the array into the right format for an ini-file. You can find such a helper-function here.

Also you are loading the file-helper from codeigniter but using the php built-in methods. Please have a look to the docs here.

Community
  • 1
  • 1
SebTM
  • 370
  • 1
  • 10
1

Something akin the following might work:

//Replace the fwrite($file,implode('', $insert)); with
$iniContent = "";
foreach ($insert as $key => $value) {
    $initContent .= $key."=".$value.PHP_EOL;
}
fwrite($file,$iniContent);

Note: This is probably the simplest thing one can do. I doesn't deal with sections or comments or escape characters or basically any sort of error checking. If you expect this sort of thing to be done a lot in your code I suggest you look into existing INI reading/writing libraries.

Update Try http://pear.php.net/package/Config_Lite or http://pear.php.net/package/Config as suggested at create ini file, write values in PHP (which also has a lot more information to look at for this particular issue).

Community
  • 1
  • 1
apokryfos
  • 38,771
  • 9
  • 70
  • 114