2017-04-02 48 views
0

我被卡住了從codeigniter更新圖像,我想要替換圖像時,我更新它但代碼其不起作用。 這裏是我的代碼:更新codeigniter上的圖像

這是控制器:

public function updatefoto(){ 
    $this->load->model('model_users'); 
    $this->model_users->updatefoto(); 

} 

這是模型

public function updatefoto($userId){ 

    if($this->input->post('submit')){ 
     $config['upload_path'] = "./uploads/images/"; 
     $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
     $config['max_size']  = '2000'; 
     $config['max_width']  = '2000'; 
     $config['max_height']  = '2000'; 
     $config['file_name']  = 'gambar-'.trim(str_replace(" ","",date('dmYHis'))); 
     $this->load->library('upload', $config); 

     if (!$this->upload->do_upload("gambar")) { 
      $error = array('error' => $this->upload->display_errors('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>','</div>')); 

      $this->load->view('view_ubah_data-profile', $error); 
     }else{ 
      $nama=$this->upload->data('file_name'); 
      $this->db->where('id', $userId); 
      $this->db->update('foto',array('nama_foto'=>$nama)); 

      redirect('view_ubah_data-profile','refresh'); 
     } 
     } 
    } 

,最後是視圖

  <div class="col-md-4 col-sm-6 col-xs-12"> 
     <form action="users/updatefoto" method="post" id="updateUserImage"> 
      <div class="text-center"> 

      <img src="<?php echo base_url();?>uploads/images/<?php echo $userData['nama_foto'] ?>" cclass="img-thumbnail" alt="avatar"> 
      <h6>Upload a different photo...</h6> 
      <input type="file" class="text-center center-block well well-sm" name="gambar" id="files" accept="image/*" required> 
      <button type="submit" value="Upload" name="submit" class="btn btn-primary"> 
      <i class="fa fa-upload" aria-hidden="true"></i>upload 
      </button> 
      </div> 
     </form> 
     </div> 

謝謝,我感謝所有幫助:)

+0

更新在控制器中的圖像不在模型 –

+0

請遵循https://www.codeigniter.com/ userguide3/libraries/file_uploading.html –

+0

@MalikMudassar我想更新圖片不插入/上傳圖片/ – Firm

回答

0

這裏是我如何做到這一點。

編輯 - 類構造

class Team extends CI_Controller { 
function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('form_validation'); 
    $this->load->helper(array('form', 'url')); 
    $this->load->model('team_model'); 
} 


public function do_upload() 
{ 
    $config['upload_path']   = './uploads/'; 
    $config['allowed_types']  = 'gif|jpg|png'; 
    $config['max_size']    = 100; 
    $config['max_width']   = 1024; 
    $config['max_height']   = 768; 

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

    if (! $this->upload->do_upload('userfile1')) 
    { 
     $data['errors'] = array('error' => $this->upload->display_errors()); 
     $data['client_Id']=$this->team_model->getUserId($email); 
     $data['pic']=$this->team_model->getImage($data['client_Id']['id']); 
     // Here I load my Views 
    } 
    else 
    { 
     $upload_data = $this->upload->data(); 
     $file_name=$upload_data['file_name']; 
     $email=$this->session->userdata['email']; 
     $data['client_Id']=$this->team_model->getUserId($email); 
     $data['pic']=$this->team_model->getImage($data['client_Id']['id']); 
     // If there is no image insert one other wise update 
     if($data['pic']['image']!=''){ 
      $this->team_model->updateEmployeeImage($data['pic']['id'],$file_name); 
      $data['success']='Congratulations! Image Updated Successfully'; 
     }else{ 
      $this->team_model->InsertEmployeeImage($data['client_Id']['id'],$file_name); 
      $data = array('upload_data' => $this->upload->data()); 
      $data['success']='Congratulations! Image Uploaded Successfully'; 
     } 
     $file_name=$upload_data['file_name']; 
     $email=$this->session->userdata['email']; 
     $data['client_Id']=$this->team_model->getUserId($email); 
     $data['pic']=$this->team_model->getImage($data['client_Id']['id']); 

     // Here I load my Views 
    } 
} 
} 

編輯 - 模型函數

public function InsertEmployeeImage($id,$image){ 
    $Image=array(
     'id'     => '', 
     'team_id'    => $id, 
     'image'     => $image 
    ); 
    $this->db->insert('teams_image',$Image); 
    return ; 
} 

public function updateEmployeeImage($id,$img){ 
    $this->deleteOldImage($id); 
    $pic=array(
     'image'  => $img 
    ); 
    $this->db->WHERE('id',$id)->update('teams_image',$pic); 
} 
+0

中刪除,並且此功能位於我的控制器 –

+0

你只是把這個代碼?沒有模型? – Firm

+0

@Firm模型函數應該用來與數據庫交互,發送數據並從數據庫中獲取數據。其餘的我們在Controller中做的事情。 –