2017-07-29 65 views
2

自從1年以來,我正在開發CI開發人員,並且我沒有遇到這種類型的錯誤。我試圖通過郵件獲取圖像名稱,但它會給我像這樣的圖像:c:\ fakelink \ imagename,我不知道什麼問題有助於我解決這個問題。消息:CI圖像上傳中的未定義索引

這裏是我的視圖代碼:

<form id="profile_update" enctype="multipart/form-data"> 

        <div class="form-group"> 
         <img src="<?php echo base_url()?>public/assets/plugins/images/users/varun.png" class="thumb-lg img-circle" alt="img"> 
         <div class="row"> 
          <div class="form-group col-sm-6"> 
           <input type="file" class="form-control" id="user_image" name="user_image"> 
          </div> 
         </div> 
        </div> 
        <div class="form-group"> 
         <button type="submit" class="btn btn-primary">Submit</button> 
        </div> 
       </form> 

這裏是Ajax代碼:

type: "POST", 
url: base_url + "admin/update_profile", 
data: 'user_image=' + user_image, 

這裏是型號代碼:

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

    if($_FILES['user_image']['name'] != '') { 
     $image = 'user_image'; 
     $upload_data = $this->do_upload($image, $config); 
     if ($upload_data['condition'] == 'error') { 
      echo json_encode(array('condition' => 'error', 'message' => $upload_data['error'] . ' (Profile image)')); 
      exit; 
     } else { 
      $account_array['foto'] = $upload_data['upload_data']['file_name']; 
     } 
    } 

錯誤位於這條線:

if($_FILES['user_image']['name'] != '') { 

感謝大家的幫助。

回答

0

您的Ajax代碼不正確。這些行添加到您的Ajax:

var formdata = new FormData ($('#profile_update')[0]); //get form data using HTML5 FormData Object 

$.ajax({ 
    type: "POST", 
    url: base_url + "admin/update_profile", 
    data: formdata, 
    contentType: false, //set content type to false so that the browser will set the content type to multipart 
    processData: false, //Also set process data to false so that the data being sent will not be serialized 
    success: function(res){ 
     //Success function goes here.. 
    } 

}); 

使用**名user_image代替ID **