表单在codeigniter

时间:2018-03-07 05:59:42

标签: jquery html css codeigniter

我正在尝试使用ajax上传图像,图像上传并正确存储到数据库,它在uploads文件夹中上传两次。

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;              
}

1 个答案:

答案 0 :(得分:1)

你曾使用过$this->upload->do_upload("file")

试试这个:

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;
}