• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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!

#2
[eluser]InsiteFX[/eluser]
It is bad practice to save images into the database!

Just save the filename into the database.

InsiteFX

#3
[eluser]Jônatan fróes[/eluser]
Active Record Class auto scape querys, so you'll have to write manually.

#4
[eluser]Bart v B[/eluser]
i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink

#5
[eluser]Jônatan fróes[/eluser]
[quote author="Bart v B" date="1287515752"]i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink[/quote]


I also agree, but sometimes its not possible...

#6
[eluser]Bart v B[/eluser]
[quote author="Jônatan fróes" date="1287519696"][quote author="Bart v B" date="1287515752"]i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink[/quote]


I also agree, but sometimes its not possible...[/quote]

What's not possible?
store the image names into a database?
Or am i reading your reply wrong?

#7
[eluser]Jônatan fróes[/eluser]
[quote author="Bart v B" date="1287522569"][quote author="Jônatan fróes" date="1287519696"][quote author="Bart v B" date="1287515752"]i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink[/quote]


I also agree, but sometimes its not possible...[/quote]

What's not possible?
store the image names into a database?
Or am i reading your reply wrong?[/quote]


of course its possible store the image names into a db, but in some projects its necessary to save the image as blob...

#8
[eluser]Bart v B[/eluser]
[quote author="Jônatan fróes" date="1287524476"][quote author="Bart v B" date="1287522569"][quote author="Jônatan fróes" date="1287519696"][quote author="Bart v B" date="1287515752"]i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink[/quote]


I also agree, but sometimes its not possible...[/quote]

What's not possible?
store the image names into a database?
Or am i reading your reply wrong?[/quote]


of course its possible store the image names into a db, but in some projects its necessary to save the image as blob...[/quote]

I can not imagine any kind of project/application you need to store images into a database. First of all it's a big opperation for a database to fetch the data, and second a database is not designed for that kind of operations (in my opionion ofcourse).
If you are affraid that they steal your images, then you beter can not put it online i think. But maybe you can explain some more why there are applications/projects that need to?

#9
[eluser]Jônatan fróes[/eluser]
[quote author="Bart v B" date="1287526376"][quote author="Jônatan fróes" date="1287524476"][quote author="Bart v B" date="1287522569"][quote author="Jônatan fróes" date="1287519696"][quote author="Bart v B" date="1287515752"]i agree with InsiteFX, bad idea to put images into a database.
A better practice is to put the image name into your database.
Also much easyer. Wink[/quote]


I also agree, but sometimes its not possible...[/quote]

What's not possible?
store the image names into a database?
Or am i reading your reply wrong?[/quote]


of course its possible store the image names into a db, but in some projects its necessary to save the image as blob...[/quote]

I can not imagine any kind of project/application you need to store images into a database. First of all it's a big opperation for a database to fetch the data, and second a database is not designed for that kind of operations (in my opionion ofcourse).
If you are affraid that they steal your images, then you beter can not put it online i think. But maybe you can explain some more why there are applications/projects that need to?[/quote]

some month ago I worked in a web module for an delphi/firebird app where all images was storage in blob format (and I had to keep it)...

sorry for my bad english Sad

#10
[eluser]InsiteFX[/eluser]
If you must do it read this!

MySQL Storing Images in Database Table

InsiteFX


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.