I would like to know if possible in my php codeigniter modelto be able to insert my file names where it matches up with the link insert post. The link post are in array i.e. <input type="text" name="link[]" />

Not sure whats best the name of the file inserts but over does not insert correct name. The input for user file is <input type="file" name="userfile+row_id" the row id produces auto generated number.


public function add_banner_image($banner_id = 0) {
        foreach ($this->input->post('link') as $link) {

        $data = array(
            'banner_id' => $banner_id,
            'link' => $link,

        foreach($_FILES as $field => $file) {
            if (!empty($file)) {

                if ($this->upload->do_upload($field)) {

                    $file_data = $this->upload->data();

                    $data['image'] = $file_data['file_name'];


        $this->db->insert('banner_image', $data);



**View **

<div class="panel-body">
<?php echo $this->load->view('flashdata/flashdata.tpl');?>
<?php if ($error_warning) { ?>
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<?php echo $error_warning; ?>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">', '</div>'); ?>
<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="form-banner" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-banner-name">Banner Name</label>
<div class="col-sm-10">
<input type="text" name="name" value="" placeholder="Banner Name" id="input-banner-name" class="form-control" />
<?php if ($error_name) { ?>
<div class="text-danger"><?php echo $error_name; ?></div>
 <?php } ?>

<div class="form-group">
<label class="col-sm-2 control-label" for="input-status">Banner Status</label>
<div class="col-sm-10">
<select name="status" id="input-status" class="form-control">
<option value="1">Enable</option>
<option value="0">Disabled</option>
<table name="images" class="table table-bordered">

var row_id = <?php echo $row_id;?>; // Int from controller.

jQuery(document).ready(function() {
  $('button[name="new-title"]').on('click', function(){
    var table = $('table[name="images"]');
    var col_1 = $('<input />', {'name': 'title[]', 'class' : 'form-control', 'type': 'text', 'placeholder': 'Title'});
    var col_2 = $('<input />', {'name': 'link[]', 'class' : 'form-control', 'type': 'text', 'placeholder': 'URL'});
    var col_3 = $('<img src="" id="previewHolder'+ row_id +'"/>');
    var col_4 = $('<input />', {'type': 'file', 'id': 'file'+ row_id, 'name': 'userfile'+ row_id, 'size': '20'});


function create_table_row($table,cols){
    $row = $('<tr/>');
    for(i=0; i<cols.length; i++){
        $col = $('<td/>');


protected function validateForm() {
        if ((utf8_strlen($this->input->post('name')) < 3) || (utf8_strlen($this->input->post('name')) > 64)) {
            $this->error['name'] = 'Name Required!';

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|png|jpg';
        $config['max_size']    = '0';
        $config['max_width'] = '*';
        $config['max_height'] = '*';
        $config['overwrite'] = TRUE;

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

        foreach($_FILES['userfile']['name'] as $field => $file) {

            if (!empty($file)) {

                if ($this->upload->do_upload($field)) {

                } else {

                    $this->error['warning'] = $this->upload->display_errors();




        return !$this->error;

I know this isn't the answer you want but I would suggest you save yourself a lot of time (and headaches) by switching to Laravel as soon as you can. CI was nice 4-5 years ago but it's time you let go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.