r/web_dev • • Aug 17 '15

Store an image file in a database

I am fairly new to web development and I have a form that users fill out and everything is getting stored to a mysql db. One of the form inputs is a file upload. I want to allow users to be able to submit images.

I know that allowing users to upload files is not the best practice but it is kind of important to the goal I am trying to accomplish. I need to be able to see the image that they submit along with the rest of the information.

What is the easiest way to store files in database?

2 Upvotes

9 comments sorted by

2

u/Sinjhin Aug 17 '15 edited Aug 17 '15

You can use blobs (Binary Large Objects), but you would need a way to retrieve and view them (another part of your website or another application entirely).

It has been a while since I have done this in PHP (which is what I assume you are using based on using a MySQL DB), but I seem to remember there being built in support for throwing blobs into a database.

https://en.wikipedia.org/wiki/Binary_large_object

1

u/xly Aug 18 '15

^ don't do this.

1

u/Sinjhin Aug 18 '15

Care to explain why? Using a CDN or some such and only storing the paths would be much faster, yes.

I am glad both of our answers are here, but his specs were to store them to a database. Using s3, azure, etc.. might be out of scope.

This is the first I have ever heard of using blobs to store images as being a bad thing. Just curious why you say so. Is it strictly for performance?

2

u/nappa300 Aug 20 '15

Last i checked reading and writing large file data to a database comes with bigger overhead tgan when writing to a file - unless it is used often.

→ More replies (2)

0

u/[deleted] Aug 17 '15

[deleted]

1

u/xly Aug 18 '15

^ do this.

1

u/MattBlumTheNuProject Sep 16 '15

Definitely do this.