Solved

Best database setup for image uploads

Posted on 2016-11-01
6
38 Views
Last Modified: 2016-11-02
I haven't yet got to learning how to upload images in php but I need to soon for a project. I was just wondering though what the best way to store details in the database would be. For arguments sake, let's say a user can upload as many images as they want for a particular product.

Should there be a table just for product images e.g.:

image_ID
image_name
image_path
product_ID

Then the images will be associated with the product ID.

Or is there another way of doing it that would be the "norm"?
0
Comment
Question by:Black Sulfur
  • 3
  • 2
6 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41868964
I think your design pattern makes perfect sense.  You might also consider a column for image_purpose and image_order.  Purpose would let you choose a featured image, etc.  Order would let you choose which image to display first, second, last, etc.

You might want to make a Google search for the exact phrase "Should I Normalize my Database" and read the very good arguments across the spectrum of opinions.
1
 

Author Comment

by:Black Sulfur
ID: 41868980
Awesome, glad to know I am on the right track.

Ah, yes. I was thinking along those lines regarding the featured image but you have made it much clearer now.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41869019
It's easy to add columns to any SQL table.  As requirements change, you can use ALTER TABLE.

You might want to learn about Laravel "Migrations" -- an interesting design.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 26

Expert Comment

by:EddieShipman
ID: 41869212
If you want to keep your storage requirements down, convert the images to base64 and save them in the DB as TEXT or VARCHAR2 data. Since you are also viewing them on the web, you can use a data url in the img tag.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41870063
@EddieShipman:  That is a joke, right?  Base64 encoding makes strings longer.  Storing large blobs in a database makes it difficult to impossible to SELECT quickly or back up the database efficiently.  You trade one form of storage (file system) for another (database) with potentially horrible consequences, so please don't do that, unless you have identified the case where this helps.  If you've identified that case and have the numbers to back up the argument, please write an article and publish it here at E-E!

Seriously, for anyone coming across this question in the future, don't put images into a database.  Or, if you're wondering why not, just try it in a test environment and see how things work out.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 41870359
No, not a joke,  I understand that the strings are long, yes. But, converting them to strings and storing them in a TEXT or VARCHAR field in a DB is MUCH smaller than storing thousands of FILES on a server.

I was just pointing out that if he didn't have storage capabilities for the images on the server, he could use that technique to store them in the DB. Why you say horrible consequences, I don't know, Ray. It is a logical thing to do when you are limited on storage space like on some shared hosting setups and need to store a lot of images.

If you still believe I'm wrong, show me.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now