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,466
  • 8
  • 24
  • 45
Rajan
  • 2,399
  • 9
  • 46
  • 100
  • 1
    `implode` doesn't write the keys of associative arrays. So if you need those in your INI this wont work. – apokryfos Feb 22 '16 at 13:29
  • You should use array_keys($insert) to get keys of $insert array. – Ravi Feb 22 '16 at 13:34
  • @apokryfos what should i be doing then? i have an array an i want to write that array in ini format – Rajan Feb 22 '16 at 13:34
  • How will that help to write .ini file? @RaviHirani – Rajan Feb 22 '16 at 13:35
  • @Rajan : I 've told you to get keys from array. I am not sure with .ini file write. I need to make demo for that. If I found anything then I will post it. – Ravi Feb 22 '16 at 13:49
  • @RaviHirani Okay thank you :) but i want to write a .ini file – Rajan Feb 22 '16 at 13:54
  • can you just check my flow ? i think there is an error due to which i dont write files here – Rajan Feb 22 '16 at 14:13

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
  • 9
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
  • 34,925
  • 8
  • 65
  • 98