Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Hochladen von Bildern in einen MySQL-Datenbank-Blob in Codeigniter

$this->input->post('photo') im Modell funktioniert nicht, um die Bildinformationen abzurufen. Weil die Bilder in $_FILES gespeichert sind, nicht in $_POST. Sie müssen also die Upload-Bibliothek verwenden in coignitor wie unten.

public function update_profile() {
       $id = $this->session->userdata('id');
       $this->load->model('edit_profile_model');

       $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);
       $this->upload->do_upload();//upload the file to the above mentioned path
       $this->edit_profile_model->update_db_user_info($id, $this->upload->data());// pass the uploaded information to the model
   } 
public function update_db_user_info($id, $imgdata) {
       $imgdata = file_get_contents($imgdata['full_path']);//get the content of the image using its path
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   } 

Um das Bild abzurufen, schreiben Sie eine Funktion in das Modell wie unten.

public function get_image($id){
       $this->db->where('id', $id);
       $result = $this->db->get('userdetails');
       header("Content-type: image/jpeg");
       echo $result['image'];
}

Und es ist auch keine gute Praxis, das Bild zu speichern und aus der Datenbank abzurufen. Versuchen Sie stattdessen, das Bild in einem Ordner zu speichern und den Pfad in der Datenbank wie unten zu speichern.

public function update_db_user_info($id, $imgdata) {
       $imgdata = $imgdata['full_path'];// get the path of the image
       $data = array(
           'fullname' => $this->input->post('fullname'),
           'address' => $this->input->post('address'),
           'state' => $this->input->post('state'),
           'city' => $this->input->post('city'),
           'pincode' => $this->input->post('pincode'),
           'image' => $imgdata,// change the type of image from blob to varchar or text
       );
       $this->db->where('id', $id);
       $this->db->update('userdetails', $data);
   }