Welcome Guest, Not a member yet? Register   Sign In
Storing images in database using active record
#1

[eluser]Unknown[/eluser]
Hello!

I have a problem storing an image into database using the active record feature of CodeIgniter.
I upload the image from a form, and store the file. And the file is ok in the filesystem.

Then, I try to save the image as a field (BLOB type) on a record of the database. The record is saved correctly, but when I check the BLOB content of the recently added file, I see some values are escaped.

For example, using HexEdit, a have this results:
Correct file:
89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
.PNG........|HDR

Incorrect file:
89 50 4E 47 0D 0A 1A 0A 5C 30 5C 30 5C 30 0D 49 48 44 52
.PNG....\0\0\0.|HDR

The method (in the controller) I'm using to generate the file:
Code:
function _do_upload( $form_field, $types, $max_size = '', $max_width = '', $max_height = '' )
    {
        $x = explode('.', $_FILES[ $form_field ]['name']);
        $ext = '.' . end( $x );
        
        $config['upload_path'] = sys_get_temp_dir();
        $config['file_name'] = uniqid( rand(), true ) . $ext;
        $config['allowed_types'] = $types;
        $config['max_size']    = $max_size;
        $config['max_width']  = $max_width;
        $config['max_height']  = $max_height;
        
        $this->load->library( 'upload', $config );
    
        if ( ! isset( $_FILES[ $form_field ] ) )
        {
            return;
        }
        
        if ( ! $this->upload->do_upload( $form_field ) )
        {
            return array( 'error' => $this->upload->display_errors() );
        }
        
        $upload_data = $this->upload->data();

        $fp = fopen( $upload_data[ 'full_path' ], 'r' );
        $file = fread( $fp, filesize( $upload_data[ 'full_path' ] ) );
        fclose( $fp );
        
        return $file;
    }

The method to generate the record (extract):
Code:
$cover = $this->_do_upload( 'cover', 'gif|jpg|png', '1024', '480', '640' );
$book = array(
            'title' => $this->input->post( 'title' ),
            'cover' => $cover
);
$id = $this->book_model->save( $book );

And the method from the model to store the book:
Code:
function save( $libro )
{
    $this->db->insert( $this->tbl_name, $book );
    return $this->db->insert_id();
}

How can I prevent escaping the binary data?
Thank you!


Messages In This Thread
Storing images in database using active record - by El Forum - 10-18-2010, 05:44 AM
Storing images in database using active record - by El Forum - 10-18-2010, 10:03 PM
Storing images in database using active record - by El Forum - 10-19-2010, 06:59 AM
Storing images in database using active record - by El Forum - 10-19-2010, 08:15 AM
Storing images in database using active record - by El Forum - 10-19-2010, 09:21 AM
Storing images in database using active record - by El Forum - 10-19-2010, 10:09 AM
Storing images in database using active record - by El Forum - 10-19-2010, 10:41 AM
Storing images in database using active record - by El Forum - 10-19-2010, 11:12 AM
Storing images in database using active record - by El Forum - 10-19-2010, 12:49 PM
Storing images in database using active record - by El Forum - 10-19-2010, 03:12 PM
Storing images in database using active record - by El Forum - 10-19-2010, 03:53 PM
Storing images in database using active record - by El Forum - 10-19-2010, 04:12 PM
Storing images in database using active record - by El Forum - 10-19-2010, 04:28 PM
Storing images in database using active record - by El Forum - 10-21-2010, 02:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB