0

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.

7
  • 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. Commented Dec 3, 2013 at 8:33
  • I get full path using file_get_contents($data['full_path']); Commented Dec 3, 2013 at 8:38
  • 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. Commented Dec 3, 2013 at 8:39
  • but $data['full_path'] return file it self without any information, if it was not, so what shall I use. Commented Dec 3, 2013 at 8:48
  • what is the output of $data['full_path']? Commented Dec 3, 2013 at 8:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.