You did not select a file to upload, file upload error in Codeigniter

3.1k Views Asked by At

I'm trying to add singer information with image. the information can be successfully added without image but if i try to insert data with image, error occurred with "You did not select a file to upload" message. My controller function is this...

   public function add_edit_singer($id = '')
   {
    if($this->input->post('save_singer_info')){

        $post=$this->input->post();
        $add=array(
        'name'=>$post['sname'],
        'bio'=>$post['bio']
        );

        $img1=$_FILES['photo']['name'];
       if($img1){
        $config['upload_path'] = 'images/singer/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';

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

        if (!$this->upload->do_upload())
        {
            $error = $this->upload->display_errors();
            $this->session->set_flashdata('info',$error);
            redirect('admin/singer_manager');
        }
        $data = array('upload_data' => $this->upload->data());
        $image = $data['upload_data']['file_name'];
        $add['image']=$image;

        }
        else{
            $add['image']='no_image.jpg';
        }

        $table='tbl_singer';
        if($this->singer_manager->add($add,$table)){
         $this->session->set_flashdata('info',"Artist/Band Information Added Successfully.");
         redirect('admin/singer_manager');
        }
        else{
            redirect('admin/singer_manager');
        }

    }
    else{
    $data['main_content']='admin/singer/add_edit_singer_view';
    if($id != '')
            $data['editdata'] = $this->common_model->get_where('tbl_singer', array('id' => $id));
    $this->load->view('admin/include/template_view',$data);
  }

}

my Model function is...

function add($data,$table){
    $name = $data['name'];
    $res = $this->db->insert($table,$data);
    if($res)
    {
        return true;
    }
    else
    {
        return false;
    }
}

and my Form is...

<form role="form" enctype="multipart/form-data" action="<?php echo base_url('admin/singer_manager/add_edit_singer/' . $id); ?>"
                          method="post"
                          id="user_form">
                        <table class="table table-hover">
                            <tr>
                              <td>Singer Name</td>
                              <td>
                              <input type="text" class="form-control required" name="sname"
                                 value="<?php if (isset($name)) {
                                     echo $name;
                                 } ?>"/>
                              </td>  
                            </tr>

                            <tr>
                              <td>Bio</td>
                              <td>
                                <textarea class="form-control required" name="bio">
                                  <?php if (isset($bio)) {
                                           echo $bio;
                                       } ?>
                                </textarea>
                              </td>
                            </tr>

                            <tr>
                              <td>Image</td>
                              <td>
                              <?php if (isset($image)) {?>
                                <div class="row">
                                  <div class="col-xs-6 col-md-4">
                                    <a href="<?php echo base_url().'images/singer/'.$image;?>" class="thumbnail">
                                      <img src="<?php echo base_url().'images/singer/'.$image;?>" width="120" height="140" id="img_prev" />
                                    </a>
                                  </div>
                                </div>
                              <?php
                               } ?>

                              <input type="file"  name="photo" size="20" />
                              </td>
                            </tr>

                            <tr>
                              <td colspan="2">
                                <?php if (isset($id)) { ?><input type="hidden" name="id" value="<?php echo $id; ?>" /><?php } ?>
                                <input type="submit" class="btn btn-success" id="btn_save" name="save_singer_info"value="Save"/>
                                <input type="reset" class="btn btn-info" id="reset"/>
                                <input type="button" class="btn btn-danger" value="Cancel"onclick="history.go(-1)"/>
                              </td>
                            </tr>

                        </table>
                    </form>
1

There are 1 best solutions below

0
Shah Rukh On BEST ANSWER

change

$this->upload->do_upload()

to

$this->upload->do_upload('photo')

may be it solve the problem