Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8665507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:29:47+00:00 2026-06-12T17:29:47+00:00

Have a form here with bunch of input text fields and a file upload

  • 0

Have a form here with bunch of input text fields and a file upload input that can only accept multiple images.

What needs to happen:

After selecting a bunch of images and clicking Submit button – the controller should insert the text data into one table, then upload all the images and add their file names to the images table with foreign_key linking to the text data that was inserted, and finally make the first image, that was selected, turned into a thumbnail and upload its name and foreign_key linking to the text data row into thumbnails table too.

What happens:

Images+thumbnail get uploaded properly(or put into the upload folder without any duplicates and etc.), but for some reason the last image that was selected gets thumbnailed, not the first one.

Also, although only one image is thumbnailed – the database ends up adding name of every image, that were selected/uploaded, to the thumbnails table with _thumb in their names.

crud.php (controller)

function add()
        {
            //Set validation properties
            $this->_set_fields();

            //Set common properties
            $data['title'] = 'Add new data row';
            $data['message'] = '';
            $data['action'] = site_url('crud/addDataRow');
            $data['link_back'] = anchor('crud/index', 'Back to list', array('class' => 'back'));

            //Load the view
            $this->load->view('templates/header', $data);
            $this->load->view('pages/crud_edit', $data);
            $this->load->view('templates/footer');
        }

        function addDataRow()
        {
            //Set common properties
            $data['title'] = 'Add new data row';
            $data['action'] = site_url('crud/addDataRow');
            $data['link_back'] = anchor('crud/index/', 'Back to list', array('class' => 'back'));

            //Set validation properties
            $this->_set_fields();
            $this->_set_rules();

            //Run validation
            if($this->form_validation->run() == FALSE)
            {
                $data['message'] = '';
            }
            else
            {
                //Get the text data from $_POST
                $data_row = array(
                    'title' => $this->input->post('title'),
                    'text' => $this->input->post('text'),
                    'price' => $this->input->post('price'),
                    'status' => $this->input->post('status'),
                    'type' => $this->input->post('type')
                );

                //Insert text data into table
                $id = $this->crud_model->save($data_row);

                //Now move on to image processing
                //original image upload settings
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '6000';
                $config['max_width']  = '1680';
                $config['max_height']  = '1050';
                $path_to_uploads= './assets/upload';
                $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                $arr_files = @$_FILES['images'];

                $_FILES = array();
                foreach(array_keys($arr_files['name']) as $h){
                    $_FILES["file_{$h}"] = array(
                        'name' => $arr_files['name'][$h],
                        'type' => $arr_files['type'][$h],
                        'tmp_name' => $arr_files['tmp_name'][$h],
                        'error' => $arr_files['error'][$h],
                        'size' => $arr_files['size'][$h]
                    );
                }

                //add this
                $this->upload->initialize($config);

                foreach(array_keys($_FILES) as $h) {

                    if (!$this->upload->do_upload($h)){
                        $error = $this->upload->display_errors();
                        //echo "<script>alert($error);</script>";
                        print($error); die;
                    }else{

                        $upload_data=$this->upload->data();
                        $file_name=$upload_data['file_name'];
                        $full_file_path = $path_to_uploads.'/'.$file_name;

                        $image_row = array(
                        'id_path' => $file_name,
                        'id_data_row' => $id
                        );

                        //Upload original image
                        $this->crud_model->save_image($image_row);

                        if(current($_FILES) == $_FILES['file_0']){
                            //Thumbnail config
                            $config['image_library'] = 'gd2';
                            $config['source_image'] = $full_file_path;
                            $config['create_thumb'] = TRUE;
                            $config['maintain_ratio'] = TRUE;
                            $config['width'] = 150;
                            $config['height'] = 150;

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

                            $this->image_lib->resize();

                            $thumbnail_row = array(
                            'id_path' => str_replace(".", "_thumb.", $file_name),
                            'id_data_row' => $id
                            );

                            $this->crud_model->save_thumbnail($thumbnail_row);
                        }       
                    }
                }
                //Set form input name="id"
                $this->form_validation->id = $id;

                //Set user message
                $data['message'] = '<div class="success">New data row added!</div>';
            }

            $this->load->view('templates/header', $data);
            $this->load->view('pages/crud_edit', $data);
            $this->load->view('templates/footer');
        }

crud_model.php (model)

//Add new data row
        function save($data)
        {

            $this->db->insert($this->tbl_data, $data);
            return $this->db->insert_id();
        }

        //Add the original image
        function save_image($data)
        {
            $this->db->insert($this->tbl_images, $data);
            return $this->db->insert_id();
        }

        //Add the thumbnail upload path and id of the row in data table to link them
        function save_thumbnail($data)
        {
            $this->db->insert($this->tbl_thumbnails, $data);
            return $this->db->insert_id();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:29:47+00:00Added an answer on June 12, 2026 at 5:29 pm

    It seems that your check is failing so the loop is running all the way through each time and overwriting the thumbnail with the last image looped.

    Try changing your check from

    if(current($_FILES) == $_FILES['file_0']){
    

    to

    if($h=='file_0'){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a simple form (only extract fields here) but for some reason the JQserilization
I have a form made up of pairs of text fields. The user can
I have two hidden input fields on my form that I have linked to
I have a form made in JSP, here I have multiple buttons - Approve,
i have the form, and i want to upload two files. here is the
Here's what I want to do. I have a form that the user fills
I have a bunch of name/email fields in my form like this: data[Friend][0][name] data[Friend][1][name]
I have some input values contained within a bunch of divs and a form.
I have a windows form application that needs to load a bunch of things
I have a form, with a bunch of fields, and then I have a:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.