I have read that using BLOB to save a path of a picture to a database is the best rather than using the database to save the picture.
Can someone please show me some code on how to do this? Like if I had users registered on my database, how would they upload their picture to their profile and if they click on their own profile, it will also retrieve their picture as well as their information on my database?
EDIT
I’m using PHP and actually have just normal fill in form and process form. I am about to make a browse ‘picture’ button on the form as well but don’t know how to process the rest by insert it.
My database would later on hold more pictures and would be very big as loads of people would insert pictures and diagrams etc. So I thought the database would just hold the path as it would process much quicker.
Not sure what to do, something like insert picture into database then save it onto my folders somewhere and delete the picture from the database and have the path of the picture in that folder saved in my database?
It sounds like you’re slightly confused about database blobs, images, and paths. Let’s step through this.
When your users upload images of significant size, you’ll want to store them in the filesystem, and record the path in the database. The path is just a string – you can use a plain old
VARCHARtype of column for it.If you wanted to store the image itself in the database, you’d use a
BLOBtype of column. A blob is a “binary large object” – it’s how you’d store data that’s not some flavor of string or number. If your users’ profile pictures are small (e.g. Twitter-sized avatars), then you might consider storing them in the database asBLOBdata.However, your main question of “okay, how do I implement this?” can’t be answered without knowing what programming language and database you’re using. You might want to provide more information so that you can get a better answer to that question.
EDIT: Responding to your clarification: yes, you should definitely store paths in the database and images in the filesystem. That’s the standard way to do it. One of the major reasons for this is that you can operate on the images separately from operating on the database. It’s much, much, much easier to back up a folder full of pictures than it is to back up a large database! You can also, for example, automatically create thumbnail versions of the images, or compare them to one another, and so on, using other applications that won’t have to touch your database.
Here’s a tutorial from Tizag about how to upload files with PHP, and here’s a tutorial from W3Schools about it. On Stack Overflow, you might find the questions “PHP Image Upload” and “Image Upload Script in PHP” helpful.