Welcome Guest, Not a member yet? Register   Sign In
Displaying image from database corrupts CI session
#1

[eluser]skunkbad[/eluser]
I don't normally store images in a database, but I have a need to store one temporarily. I first upload the image:

Code:
$tmp_name = $_FILES['userfile']['tmp_name'];
$handle = fopen( $tmp_name, "r" );
$image_string = base64_encode( fread( $handle, filesize($tmp_name) ) );
fclose($handle);

Then encrypt it:

Code:
$this->load->library('encrypt');
$this->encrypt->set_cipher( MCRYPT_BLOWFISH );
$key = sha1('mysecretkey');
$encrypted_image = $this->encrypt->encode( $image_string, $key );

The image is now an encrypted string and ready for the database update:

Code:
$this->db->where( 'record-id', $this->input->post('profile_id') );
$this->db->update( 'table_name' array( 'image' => $encrypted_image ) );

The image is stored in a longtext field.

When I want to see the image in another view, I have this in my controller:

Code:
$this->load->library('encrypt');
$this->encrypt->set_cipher( MCRYPT_BLOWFISH );
$key = sha1('mysecretkey');
$view_data['the_image'] = $this->encrypt->decode( $view_data['the_image'], $key );

And in the view displaying the image:

Code:
echo '<img  />';

The image displays fine, but every pageload after that I get the following error:

Code:
PHP E_NOTICE #8 - Date/Time: 8/6/2011 18:57:26
File: C:\xampp\htdocs\beta-cps\system\libraries\Session.php
Line: 722
unserialize() [function.unserialize]: Error at offset 0 of 319 bytes

It is definitely the displaying of the image that triggers the error, and the error persists on any page of the website until I clear my cookies and start over. Does anybody know what is happening here?


Messages In This Thread
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 08:13 PM
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 08:47 PM
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 08:58 PM
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 09:28 PM
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 09:36 PM
Displaying image from database corrupts CI session - by El Forum - 08-06-2011, 11:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB