简体   繁体   中英

How to edit an uploaded file in CodeIgniter

I am uploading an .ini file using CodeIgniter.

Once I upload the file I use PHP's INI parser and get the file contents as an array.

Now I want to edit the contents of that file. How do I do it?

My Code:

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = '*';

    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('customer/upload/upload_ini', $error);
    }
    else
    {
        $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);

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

In View I do:

<?php 
var_dump($parameters);
?>
<tr>
    <td>OTAID</td>
    <td><?php echo form_input('OTAID', set_value('OTAID', $parameters->OTAID)); ?></td>
</tr>
<tr>
    <td>SipUserName</td>
    <td><?php echo form_input('SipUserName', set_value('SipUserName', $parameters->SipUserName)); ?></td>
</tr>
<tr>
    <td>SipAuthName</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>DisplayName</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>Password</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>Password</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>Domain</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>Proxy</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>
<tr>
    <td>ServerMode</td>
    <td><?php echo form_input('email2', set_value('email2', $parameters->email2)); ?></td>
</tr>

From my file I get this contents:

array (size=9)
    'OTAD' => string '0' (length=1)
    'PName' => string '' (length=0)
    'UName' => string '' (length=0)
    'DisplayName' => string '' (length=0)
    'Password' => string '' (length=0)
    'Domain' => string '' (length=0)
    'Proxy' => string '' (length=0)
    'Port' => string '' (length=0)
    'ServerMode' => string 'Automatic' (length=9)

Use this Coding

 <td>OTAID</td>
        <td><?php echo form_input('OTAID', set_value('OTAID', $parameters->OTAID)); ?></td>

    </tr>



    <tr>

        <td>SipUserName</td>
        <td><?php echo form_input('SipUserName', set_value('SipUserName', $parameters['SipUserName'])); ?></td>

    </tr>


    <tr>

        <td>SipAuthName</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>

    </tr>


    <tr>


        <td>DisplayName</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>
    </tr>



    <tr>


        <td>Password</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>
    </tr>


    <tr>


        <td>Password</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>
    </tr>


    <tr>


        <td>Domain</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>
    </tr>


    <tr>


        <td>Proxy</td>
        <td><?php echo form_input('email2', set_value('email2', $parameter['email2'])); ?></td>
    </tr>


    <tr>


        <td>ServerMode</td>
        <td><?php echo form_input('email2', set_value('email2', $parameters['email2'])); ?></td>
    </tr>

As already described in comments you need to use $parameters in view as array because parse_ini_file returns one.

What kind of text box you want to generate? You are using a normal input field , do you mean a textarea ?

How to generate an ini-file from array you can see here . You can use file_put_contents to write into the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM