1

I have a form am submitting and user has the ability to upload a picture in that form then submit the form as whole. I found a tutorial on codeigniter site showing upload form (dedicated only to upload, not other data along). link is: Codeigniter Upload Tutorial. How can I submit the form and then upload the files while also uploading other details to other table in database?

3 Answers 3

2

Below is an example

    function add_staff(){
            $this->load->library('form_validation');
            $this->load->helper(array('form', 'url'));
            // field name, error message, validation rules
            $this->form_validation->set_rules('name', 'Full name', 'trim|required');
                            $this->form_validation->set_rules('designation', 'Last Name', 'trim|required');
            if($this->form_validation->run() == FALSE)
            {   
                $data['main_content']   =   'staff/add_staff';
                $this->load->view('admin/template',$data);
            }
            else
            {
                $config['upload_path'] = 'uploads/staff/';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';


                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if ( ! $this->upload->do_upload('file'))
                {
                                            $data['error'] = array('error' => $this->upload->display_errors());

                    $new_staff = array(
                            'name' => $this->input->post('name'),
                            'designation' => $this->input->post('designation'),
                                                            'phone' => $this->input->post('phone'),
                                                            'email' => $this->input->post('email'),
                                                            'address' => $this->input->post('address'),
                                                            'description' => $this->input->post('description'),
                            'status' => $this->input->post('status')
                    );

                }
                else
                {   
                    $file_data  =   $this->upload->data('file');

                    $new_staff = array(
                            'name' => $this->input->post('name'),
                            'designation' => $this->input->post('designation'),
                                                            'phone' => $this->input->post('phone'),
                                                            'email' => $this->input->post('email'),
                                                            'address' => $this->input->post('address'),
                                                            'photo' => $file_data['file_name'],
                                                            'description' => $this->input->post('description'),
                            'status' => $this->input->post('status')
                    );

                }                   
                    if($this->staff_model->add_staff($new_staff)):
                        $this->session->set_flashdata('success', 'Staff added Sucessfully !!');
                    endif;
                    redirect('admin/staff');                        
            }

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry sujeet, but what's the purpose of this code? It is really unclear in what purpose it serves (am also new to CI)
(1) redirect to form if not isset($_POST) (2) if isset($_POST) and meets validation, uploads files and call model to insert into db. works for add and edit form with images. Replace image if image is provided while edit. if not use current image
0

you can pass in other data as well along with file form your form to controller, like

<?php echo form_open_multipart('upload/do_upload');?>
    <input type="text" name="someField" value=""  />
    <input type="file" name="userfile" size="20" />
    <input type="submit" value="upload" />
</form>

and in your upload controller's do_upload function:

$someField = $this->input->post("somefield"); //save to some db
//and rest of your file to be uploaded code from the same link you provided

Did you mean something like this

1 Comment

will this work? Because there is a whole example on uploading file where I can check for type of images and all. So should I pass $somefield to that method and that's it?
0

Image MOO is a excellent library that can accomplish image manipulations on the fly....

e.g for resizing the uploaded image to a particular path, you just have to do this

public function thumbnailer($uploader_response,$field_info,$files_to_upload)
{
    $this->load->library('image_moo');
$file_uploaded=$field_info->upload_path.'/'.$uploader_response[0]->name;
$thumbnails=$field_info->upload_path.'/thumbnails/'.$uploader_response[0]->name;
    $cart_thumbnails=$field_info->upload_path.'/cart_thumbnails/'.$uploader_response[0]->name;
$this->image_moo->load($file_uploaded)->resize(400,250)->save($thumbnails,false);
}

Hope this helps !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.