Sudhakar SJ
Sudhakar SJ

Reputation: 111

Form upload the same file twice in uploads folder in codeigniter

I am trying to upload the image using ajax, image uploading and storing to the database correctly, it's uploading twice in the uploads folder.

if(!empty($_FILES)){
    $uploadconfig = array(
        'upload_path' => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size' => '204800',
        'file_name'  => $_FILES['file']['name'],
        'encrypt_name' => TRUE
    );

    //print_r($uploadconfig); exit;

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

    if ( ! $this->upload->do_upload("file")) {
        echo "failed to upload file(s)";
    }
    $this->upload->initialize($uploadconfig);
    $this->upload->do_upload("file");
    $upload_data = $this->upload->data();
    $user_profile = $upload_data['file_name'];

    $data = array();
    $data['first_name'] = $this->input->post('xx_first_name');
    $data['last_name'] = $this->input->post('xx_last_name');
    $data['email'] = $this->input->post('db_email');
    $data['user_dob'] = $this->input->post('db_dob');
    $data['user_zip'] = $this->input->post('user_zip'); 
    $data['user_img'] = $user_profile;              
}

Upvotes: 1

Views: 146

Answers (1)

Sinto
Sinto

Reputation: 3997

You have used $this->upload->do_upload("file") it twise.

Try this:

if (!empty($_FILES)) {
    $uploadconfig = array(
        'upload_path' => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size' => '204800',
        'file_name'  => $_FILES['file']['name'],
        'encrypt_name' => TRUE
    );

    $this->load->library('upload', $uploadconfig); // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class
    // $this->upload->initialize($uploadconfig); // I thinks its not required because you have already done it in previous line.
    if (!$this->upload->do_upload("file")) {
        echo "Failed to upload file(s)";
        $upload_data  = array(); 
        $user_profile = $_FILES['file']['name'];
        // you will error reason here on `$this->upload->display_errors()`
    } else {
        $upload_data    = $this->upload->data();
        $user_profile   = $upload_data['file_name'];
    }

    $data = array();
    $data['first_name'] = $this->input->post('xx_first_name');
    $data['last_name']  = $this->input->post('xx_last_name');
    $data['email']      = $this->input->post('db_email');
    $data['user_dob']   = $this->input->post('db_dob');
    $data['user_zip']   = $this->input->post('user_zip'); 
    $data['user_img']   = $user_profile;
}

Upvotes: 1

Related Questions