0
votes

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.

1
you should just save the image path not the image itself. otherwise you will end up with huge amount of storage in db server and bad performance. - mamdouh alramadan
I get full path using file_get_contents($data['full_path']); - Doksh
file get path would return file information, not the file itself. what would you do is either to store the image name or image path with name then just echo it. - mamdouh alramadan
but $data['full_path'] return file it self without any information, if it was not, so what shall I use. - Doksh
what is the output of $data['full_path']? - mamdouh alramadan

1 Answers

1
votes

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.