0

I am having a problem with inserting file_path to database and file upload in folder using Codeigniter, I have a form field where user can upload any jpg|png|gif file. But how will I get the file_path of that particular file uploaded and insert it into database and file are stored in floder. my controller code

public function enqry(){
            $data = array();
            $postData = array();
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; 
            $config['max_size']      = 10240;
          $this->load->library('upload', $config);
            //if add request is submitted
            if ( ! $this->upload->do_upload('upload')) {
                 $error = array('error' => $this->upload->display_errors()); 
                    $this->load->view('imageUploadForm', $error); 
                     }else { 
                      print_r('Image Uploaded Successfully.');

                  } 
            if($this->input->post('postSubmit')){

                //form field validation rules
                $this->form_validation->set_rules('name', 'post name', 'required');
                $this->form_validation->set_rules('email', 'post email', 'required');
                $this->form_validation->set_rules('mobile', 'post number', 'required');
                $this->form_validation->set_rules('nationality', 'post nationality', 'required');
                $this->form_validation->set_rules('location','post location','required');

                //prepare post data
                $postData = array(
                    'name' => $this->input->post('name'),
                    'email' => $this->input->post('email'),
                    'mobile' => $this->input->post('mobile'),
                    'nationality' => $this->input->post('nationality'),
                    'location'=>$this->input->post('location'),
                    'statuse' => '0',
                    'created_at' => date("Y-m-d H:i:s")
                    /*'passport_details'=>$this->input->post('passport_details')*/
                );

                //validate submitted form data
                if($this->form_validation->run() == true){
                    //insert post data
                    $insert2 = $this->user_mod->insert_enq($postData);
                    if($insert2){
                        $this->session->set_userdata('success_msg', 'Post has been added successfully.');

                        redirect('/User_con/log/');
                    }else{
                        $data['error_msg'] = 'Some problems occurred, please try again.';
                    }
                }
            }

            $data['post'] = $postData;
            $data['title'] = 'Create Post';
          }   
            //load the add page view
              public function log_enq(){          
            $this->load->view('templates/header');
            $this->load->view('enquiry_reg');
            $this->load->view('templates/footer');
        }

My view code

<div class="form-group">
 <label for="title">File Upload</label>
  <input type="file" name="upload" placeholder="upload">
</div>

My model code

public function insert_enq($data = array()) {
        $insert2 = $this->db->insert('tbl_enquiry', $data);
        if($insert2){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }
2

3 Answers 3

0
 if (!$this->upload->do_upload('upload')) {
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('imageUploadForm', $error); 
 }
 else {  
      $data = $this->upload->data('file_name');
      $image = 'your_path'.$data;
 } 

and add $image variable to your your query like

$arrayName = array(
    'imapge' => $image,
    'more_column' => $more_column_values);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you sir, i tried that code but not working ,it goes error
$error = array('error' => $this->upload->display_errors()); $this->load->view('user_con/error', $error); this code will be excuted
0
public function enqry(){
        $data = array();
        $postData = array();
        $postData['upload_path']   = './uploads/'; 
        $postData['allowed_types'] = 'gif|jpg|png'; 
        $postData['max_size']      = 10240;
      $this->load->library('upload', $postData);
if (!$this->upload->do_upload('upload')) {
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('user_con/error', $error); 
 }
 else {  
      $data = $this->upload->data('file_name');
      $image = 'your_path'.$data;
 } 
        if($this->input->post('postSubmit')){
$this->form_validation->set_rules('name', 'post name', 'required');
            $this->form_validation->set_rules('email', 'post email', 'required');
            $this->form_validation->set_rules('mobile', 'post number', 'required');
            $this->form_validation->set_rules('nationality', 'post nationality', 'required');
            $this->form_validation->set_rules('location','post location','required');
$postData = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'mobile' => $this->input->post('mobile'),
                'nationality' => $this->input->post('nationality'),
                'location'=>$this->input->post('location'),
                'statuse' => '0',
                'created_at' => date("Y-m-d H:i:s"),
                'upload' => $image
 );
if($this->form_validation->run() == true){
$insert2 = $this->user_mod->insert_enq($postData);
if($insert2){
                    $this->session->set_userdata('success_msg', 'Post has been added successfully.');

                    redirect('/User_con/log/');
                }else{
                    $data['error_msg'] = 'Some problems occurred, please try again.';
                }
            }
        }

        $data['post'] = $postData;
        $data['title'] = 'Create Post';
 }  

Comments

0
$config = array(
                'upload_path'   =>'./uploads/',
                'allowed_types' =>'jpg|jpeg|png|gif',
                'max_size'      => '5000KB');

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

Replace this code your code....and cheack......

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.