Inserting and then retrieving a blob data from mysql database using php

I read about one big company that did that and when the hard drive crashed the support staff restored the database from the backup which left tens of thousands of broken image links as the images were not backed up.

Another reason for including images in the database is where they may be regularly being added and removed along with other database updates as a part of the same transaction where as separate files you could easily get the images out of step with the rest of the data if you needed to do a rollback.

There are both pros and cons to including images in the database or keeping them separate. I think this is one of the situations with respect to database design where the two alternatives are just about equal in which is better - depending on the requirements of the particular use.

In this particular instance applying mysqli_real_escape_string to the image code is probably breaking the image.

I’d also get rid of the fopen and fread of the image file and use the LOAD_FILE() call inside the SQL to transfer the image into the database directly.

See http://www.felgall.com/mysql06.htm for an article I wrote about the database calls for directly loading and retrieving files in database BLOB fields.