Solved

Best database setup for image uploads

Posted on 2016-11-01
6
61 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 109

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 109

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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 109

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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