jimbo
jimbo

Reputation: 1018

Converting files to blob to save in database

I have an old user site that we are currently updating. The site's users have previously uploaded profile pics that are stored in a directory. I am now wanting to keep these profile images in the database, (easy to backup all data) but am having trouble working out how to do it.

There are a lot of tutorials talking about do ing this from files that have just been uploaded and using the tmp name etc, but how can I re-create with files that have already been uploaded?

I have looked into $data = file_get_contents($filename); Which seems to create a binary file, but doesn't seem to save in database with:

mysql_query("UPDATE profiles SET company_logo = mysql_real_escape_string('".$data."') WHERE id = 1 ") or die(mysql_error());

*UPDATE* So after looking into this some more, I think I will go with the majority and save the images as they already are... in folders..

Upvotes: 0

Views: 4696

Answers (1)

JvdBerg
JvdBerg

Reputation: 21856

It's generally a bad idea to store images in the database.

It is better to store the images in a file and keep a reference in a field or table.

However, if you want to store them, you must store them as BLOB (Binairy Large OBject). On a BLOB field, no character translation is done. When you store them, you must store them exactly as the file contents. So remove the mysql_real_escape string, as it will mess up your binary data.

Upvotes: 1

Related Questions