简体   繁体   中英

Storing and retrieving image in sql server using Codeigniter

I want to store image in SQL server using Codeigniter ; actually I can select image and convert it but when I do that and store it in database then retrieve it the image cannot display.

What I observed in database the previous name for images that stored using C# begins with 0xFFD8F but using codeigniter the name begins with 0x3 or anything else.

here the controller

public function addImage() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';

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

    if ( ! $this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('Profile/addImage', $error);
    } else {
        $data = $this->upload->data(); 
        $dataString = file_get_contents($data['full_path']);
        $orig_name = $data['orig_name'];
        $uploadImage = $this->Personalinfo_model->uploadImage(array('orig_name' => $orig_name, 'dataString' => $dataString ));
        delete_files($data['full_path']) ;
    }
}

Model

function uploadImage($options = array()) {
    $orig_name = $options['orig_name']; 
    $dataString = $options['dataString']; 
    // $hex_image = bin2hex($dataString);
    $data = unpack("H*hex", $dataString);
    $object = array(
        'EmployeeID' => '3',
        'filename' => $orig_name,
        'EmployeePic' => "0x".$data['hex']
    );
    $this->db->where('EmployeeID','3');
    $this->db->update('EmployeePic', $object);
}

After unpack image something wrong gonna happened, but i can not detect the problem.

As we discussed in the Chat side, you have the function as follows:

function uploadImage($options = array()) { 
    $orig_name = $options['orig_name']; 
    $dataString = $options['dataString']; 
    $hex_image = bin2hex($dataString); 

    $object = array( 
        'EmployeeID' => '3', 
        'filename' => $orig_name, 
        'EmployeePic' => "0x".$hex_image 
    ); 
    $this->db->where('EmployeeID','3'); 

    $this->db->update('[HumanResource].[dbo].[EmployeePic]', $object); 
}

And you are concatenating 0x string to your binary. by removing it you should have your function working properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM