0

Controller

          public function addemployee()
          {
           $config['upload_path']  = './images/employee/';
           $config['allowed_types'] = 'gif|jpg|png';
           $config['max_size'] = 100;
           $config['max_width'] = 1024;
           $config['max_height'] = 768;

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

          if ( ! $this->upload->do_upload('userfile'))
          {
           $error = array('error' => $this->upload->display_errors());
           $this->load->view('include/headerforms');
           $this->load->view('include/aside');
           $this->load->view('template/forms/employeeforms',$error);
           $this->load->view('include/footerforms'); 
          }
          else
          {
           $data = array('upload_data' => $this->upload->data('userfile'));
           $data = [
          'userfile'   => './images/employee/' . $file['file_name'],
          'userfile' => $this->input->post('userfile'),
          'firstname' => $this->input->post('firstname'),
          'firstname' => $this->input->post('firstname'),
          'middlename'=> $this->input->post('middlename'),
          'lastname'=> $this->input->post('lastname'),
          'company'=> $this->input->post('company'),
          'department'=> $this->input->post('department'),
          'position'=> $this->input->post('position'),
          'address'=> $this->input->post('address'))
          ];
          $this->ci_db_model->addemployee($data);

this portion is connect to my database

          $this->session->set_flashdata('message','New image has been added..');
          $this->load->view('include/header2');
          $this->load->view('include/aside');
          $this->load->view('template/employee');
          $this->load->view('include/footer2');
          }
         }

View

      <!-- Name -->
               <?php echo form_open_multipart('home/addemployee'); ?>
              <div class="box-body">
                <div class="form-group">
                  <label for="exampleInputFile">Photo</label>
                  <input type="file" name"userfile" id="exampleInputFile" required>
                </div>
              <div class="row">
                <div class="col-xs-4">
                    <input name="firstname" type="text" class="form-control" placeholder="First Name" required>
                  </div>
                  <div class="col-xs-4">
                    <input name="middlename" type="text" class="form-control" placeholder="Middle Name" required>
                  </div>
                  <div class="col-xs-4">
                    <input name="lastname" type="text" class="form-control" placeholder="Last Name" required>
                  </div>
                </div>      
         <input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
            <?php echo form_close(); ?>

Programming language : PHP Codeigniter server: XAMPP

I'M BEGINNER!!

Question: How to upload image into database?

When I click the submit button there an error "You did not select a file to upload.".

What wrong with my code?

THANKS IN ADVANCE PLEASE HELP ANYONE.

1
  • 1
    Try printing $_FILES['userfile']['name'] before calling do_upload. Does it store the correct filename? Commented Nov 30, 2017 at 10:16

1 Answer 1

1

controller

    if ((isset($_FILES['userfile']['name'])) && ($_FILES['userfile']['name'] != "")) {
                $file = "userfile";
                $path = './uploads/file/' ;
                $uploadedimages = $this->upload_image($file, $path, 1);
                $data['image'] = $uploadedimages['file_name'];
    }

/* Upload file function */

public function upload_image($file, $path, $thumb) {

        /* Upload file  in posted */
        $this->load->library('image_lib');
        $this->load->library('upload');

        $image_upload_folder = $path;

        if (!file_exists($image_upload_folder)) {
            mkdir($image_upload_folder, DIR_WRITE_MODE, true);
        }
        @chmod($image_upload_folder, 0777);
        $this->upload_config = array(
            'upload_path' => $image_upload_folder,
            'allowed_types' => '*',
            'remove_space' => TRUE,
            'encrypt_name' => TRUE,
        );

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

        if (!$this->upload->do_upload($file)) {
            $upload_error = $this->upload->display_errors();
            echo ($upload_error);

        } else {
            $file_info = $this->upload->data();

            if ($thumb == 1) {

                $this->image_lib->clear();
                $config['image_library'] = 'GD2';
                $config['source_image'] = $image_upload_folder . '/' . $file_info['file_name'];
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '_thumb';
                $config['master_dim'] = 'width';
                $config['quality'] = 100;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 150;
                $config['height'] = 150;
                $config['new_image'] = $file_info['file_name'];

                //$this->image_lib->clear();
                //move_uploaded_file($value['full_path'], '/uploads/' . $newimagename);
                $this->image_lib->initialize($config);
                //$this->load->library('image_lib', $config);
                if (!$this->image_lib->resize()) {

                    $this->image_lib->clear();
                    $data['file_name'] = $file_info['file_name'];
                    $data['file_thumb'] = $file_info['file_name'];


                } else {
                    $upload_error = $this->image_lib->display_errors();
                                       $data['file_name'] = $file_info['file_name'];
                    $thmb = explode(".", $data['file_name']);
                    $data['file_thumb'] = $thmb[0] . "_thumb." . $thmb[1];
                }


                return $data;
            } else {
                return $file_info['file_name'];
            }
        }
    }
Sign up to request clarification or add additional context in comments.

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.