2015-10-14 126 views
2

嘿,我想上传一个图像到我的资产文件夹。但我无法正确完成。实际上我想用名称保存图像“最后插入的ID”。 我不知道错误是什么。请帮我如何上传图像codeigniter

$.ajax({ 
       url: base_url+"register/reg_submit/", 
       data: $('#career_submit_form').serialize(),//returns all cells' data 
       dataType: 'json', 
       type: 'POST', 
       success: function (res) { 
        alert(res.result) 
    }, 

在我的控制器reg_submit

 $application_insert = $this->career->insert_student_application($personal_details,$parent_details,$other_details); 

    $config['upload_path'] = './application/assets/images/user_image/'; 
    $config['allowed_types'] = 'jpg'; 
    $config['file_name'] = $application_insert.'.jpg'; 
    $this->load->library('upload', $config); 
    if (!$this->upload->do_upload('userfile')) 
    {  
     echo $this->upload->display_errors(); 

    } 

回答

0

试试这个:

public function file_upload(){ 
       $files = $_FILES; 
       $cpt = count($_FILES['userfile']['name']); 
       for($i=0; $i<$cpt; $i++) 
       { 
       $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
       $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
       $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
       $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
       $_FILES['userfile']['size']= $files['userfile']['size'][$i]; 
       $this->upload->initialize($this->set_upload_options()); 
       $this->upload->do_upload(); 
       $fileName = $_FILES['userfile']['name']; 
       $images[] = $fileName; 
} 
    $fileName = implode(',',$images); 
    $this->my_model->upload_image($fileName); 
} 
private function set_upload_options() 
    { 
    // upload an image options 
     $config = array(); 
     $config['upload_path'] = './upload/'; //give the path to upload the image in folder 
     $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '0'; 
     $config['overwrite'] = FALSE; 
    return $config; 
    } 

欲了解更多如何上传笨图片试试这个:http://w3code.in/2015/09/upload-file-using-codeigniter/

+0

sry它不起作用。 –

+0

你正在得到什么错误?你能够将图像名称存储在数据库中吗? – Ricky

+0

我只是想保存图像与“最后插入id.jpg”作为名称到我的图像文件夹 –

0

请按照以下步骤

1. check $application_insert variable return anything? 

2. change './application/assets/images/user_image/' to "application/assets/images/user_image/" 

3. make writable all the folders assests, images and user_image 

4. check spelling of the folders are same what you given in the upload path