Dhara Vihol
Dhara Vihol

Reputation: 612

Can not upload image in the folder using Codeigniter

I am trying to upload image to the folder on the server and store another data in the databse from the same form simultaneously. But it is showing error You did not select a file to upload.

Below is my view and controller for the same:

View:

<form role="form" method="post" action="job_insert" enctype="multipart/form-data">
                        <div class="box-body">
                          <div class="form-group">
                              <label>Job Name*</label>
                              <input type="text" class="form-control" name="jname" placeholder="Enter Job Name" required>
                          </div>

                          <div class="form-group">
                            <label>Description</label>
                            <textarea rows="4" cols="10" class="form-control" name="description" required></textarea>
                          </div>
                          <div class="form-group">
                              <label>File input</label>
                            <input type="file" name="userimage">
                          </div>
                          <div class="form-group">
                      <label>Paper Size*</label>

                  <div class="form-group">
                    <label>Paper Cutting Size</label>  
                      <div>
                      <input id="cutting_size" name="cutting_size" type="text" placeholder="Cutting Size" class="form-control input-md" required>  
                    </div>
                  </div>
                  <div class="form-group">
                    <label>Sheet</label>  
                    <div>
                      <input id="sheet" name="sheet" type="text" placeholder="No. of Sheet" class="form-control input-md" required>
                      </div>
                  </div>
                  <div class="form-group">
                    <label>Tin No.</label>  
                    <div>
                      <input id="sheet" name="tin" type="text" placeholder="Tin No." class="form-control input-md" required>
                      </div>
                  </div>
                    </div>
          <!-- /.box-body -->
                    <div style="margin-left: 600px" class="box-footer">
                      <button type="submit" class="btn btn-primary">Submit</button>
                    </div>
                </form>

Controller:

public function job_insert()
        {
            $this->form_validation->set_rules('jname', 'job_name', 'trim');
            if($this->form_validation->run()== FALSE)
            {
                $this->load->view('invalid_jobname');
            }
            else
            {
                $this->do_upload();

                $data = $_SESSION['email'];
                $id = $this->user_model->get_client_id($data);
                $data = array('cid' => $id,
                                'job_name' => $this->input->post("jname"),
                                'job_id' => $this->input->post("job_id"),
                                'description' => $this->input->post("description"),
                                'img_name' => $img_name,
                                'adate' => Date('Y-m-d'),
                                'status' => 'NEW',
                                'paper_size' => $this->input->post("paper_size"),
                                'paper_type' => $this->input->post("paper_type"),
                                'cutting_size'=> $this->input->post("cutting_size"),
                                'sheet'=> $this->input->post("sheet"),
                                'tin' => $this->input->post("tin"),
                                'lamination' => $this->input->post("checkboxes"),
                                'print_type' => $this->input->post("checkboxes1"),
                                'binding' => $this->input->post("checkboxes2"),
                                 );

                $query = $this->user_model->job_insert($data);
                if ($query == TRUE) 
                {
                    $this->load->view('job_submitted');
                }
                else
                {
                    $this->load->view('error_page');
                }

            }
        }

Here's my do_upload function:

public function do_upload()
    {
    $config = array(
                'upload_path' => "./uploads/",
                'allowed_types' => "gif|jpg|png|jpeg|pdf",
                'overwrite' => FALSE,
                'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                'max_height' => "768",
                'max_width' => "1024"
                 );

                $this->load->library('upload', $config);
                $image = $this->input->post("image");
                echo $image;
                if(!$this->upload->do_upload('userimage'))
                {   
                    $error = $this->upload->display_errors();
                    echo $error;
                }
        }

Upvotes: 0

Views: 571

Answers (3)

Shibon
Shibon

Reputation: 1574

Change this to

<input type="file" name="image">

to

<input type="file" name="userfile"/>

and in do_upload

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

to if ( ! $this->upload->do_upload('userfile'))

Upvotes: 1

AmmyTech
AmmyTech

Reputation: 738

update form tag with below code

<form role="form" method="post" action="job_insert" enctype="multipart/form-data">

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Provide enctype="multipart/form-data" in your form tag. Without it you cannot upload image.

Upvotes: 0

Related Questions