On my view I am trying to get it so if my input link[] is next to the upload button then that link post will be inserted into the database with image I use codeigniter for a mvc frame work.

I can insert the links and images fine, but the link/url that I type in, in each link input are not matching the uploaded filename. How can I resolve this?

Thanks In Advance

function do_upload() {

        $files = $_FILES;
        $file_loop = count($_FILES['userfile']['name']);

        for($i= 0; $i < $file_loop; $i++) {

            $_FILES['userfile']['name'] = $files['userfile']['name'][$i];
            $_FILES['userfile']['type'] = $files['userfile']['type'][$i];
            $_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
            $_FILES['userfile']['error'] = $files['userfile']['error'][$i];
            $_FILES['userfile']['size'] = $files['userfile']['size'][$i];


            $this->upload->initialize($this->file_config());

            if (!$this->upload->do_upload()) {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('design/banner_form.tpl', $error);
            } else {


                $data['link'] = $this->input->post('link');

                if (isset($data['link'])) {
                    foreach ($data['link'] as $link) {
                        $data = array(
                            'link' => $link,
                            'image' => $files['userfile']['name'][$i]
                        );
                    }
                }

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

                $this->load->view('design/banner_list.tpl', $data);
            }
        } 
    }

View

<?php echo form_open_multipart('admin/design/banners/do_upload');?>

<table>
    <thead>
        <tr>
            <td>Link</td>
            <td>Image</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="text" name="link[]" /></td>
            <td><input type="file" name="userfile[]" size="20" multiple="multiple" /></td>
        </tr>
        <tr>
            <td><input type="link" name="link[]" multiple="multiple" /></td>
            <td><input type="file" name="userfile[]" size="20" multiple="multiple" /></td>
        </tr>
    </tbody>
</table>

<input type="submit" value="upload" />

</form>

I have found the best way to match up url / link with the image. Becaue both url and image are in array I have had to
create two lots of for for($i= 0; $i <; $i++) {} one for the file upload and the other for the link post array.

After created for($i= 0; $i <; $i++) {} for link then I copyied the files['link']['name'][$i] and then set that in
the loop. It all seems to work now.

I can now match up the image with the correct url/link. Did a lot of research and trial and error.

function do_upload() {

$files = $_FILES;
$file_loop = count($_FILES['userfile']['name']);

for($i= 0; $i < $file_loop; $i++) {

$_FILES['userfile']['name'] = $files['userfile']['name'][$i];
$_FILES['userfile']['type'] = $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name'] = $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error'] = $files['userfile']['error'][$i];
$_FILES['userfile']['size'] = $files['userfile']['size'][$i];


$this->upload->initialize($this->file_config());

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

$error = array('error' => $this->upload->display_errors());

$this->load->view('design/banner_form.tpl', $error);

} else {

$data_count = count($_POST['link']);

for ($i=0; $i <$data_count ; $i++) { 

$url = $_POST['link'][$i];

$filename = $files['userfile']['name'][$i];

$this->db->set('link', $url);

$this->db->set('image', $filename);

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

}

$this->load->view('design/banner_list.tpl');

}

} 

}
Be a part of the DaniWeb community

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