Search code examples
phpcodeigniter-2

Form is not submitting properly in codeigniter


I have form which is suppose to post information and upload images, when i post information the form is doing what it should do but when i browse photos it post noting, neither information nor files, can anyone help me why is it doing so? Actually i have 7 file fields in the form with a single one it iw working but with multiple it is not working.

<?php $attributes = array("name" => "project_add");
echo form_open_multipart("project/add", $attributes);?>
<?php echo $this->session->flashdata('msg'); ?>
Project Name <span>*</span><br/>
<input class="form-control" name="name" id="name" type="text"value="<?php echo set_value('name'); ?>" /><br/>
<input class="form-control" name="f_lcpd" id="f_lcpd" type="checkbox" />     <label for ="">Painted Desert</label>`
Invoice <input class="form-control" name="userfile[]" id="" type="file"  />
image 1<input class="form-control" name="userfile[]" id="" type="file"  />
Image 2<input class="form-control" name="userfile[]" id="" type="file"  />
<input type="submit" value="Save" />
<?php echo form_close(); ?>

controller function

public function add()
{
print_r($_POST);
print_r($_FILES);
}

Solution

  • You should use enctype="multipart/form-data" (to form) when you submit files to server. On server side, you can access these files using $_FILES supergloal and you can access standard POST data using $_POST (only if method="POST")

    If you want to submit multiple files, your code should looks like this:

    <input name="userfile[]" type="file" /><br />
    <input name="userfile[]" type="file" /><br />
    

    And then, on server side (if you are not using any helper):

    foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name)
         // process image upload, e.g. if you want the size: $_FILES['userfile']['size'][$key]
    }
    

    You can use the above solution in conjunction with do_upload method in CodeIgniter.