Solved

Best database setup for image uploads

Posted on 2016-11-01
6
67 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

856 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