在codeigniter上更新图像

时间:2017-04-02 06:46:23

标签: php codeigniter updates crud

  

我停止从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>
  

谢谢,我感谢任何帮助:)

1 个答案:

答案 0 :(得分: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);
}