Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store the location of an image in a database?

I have a list of colors, with textures that I want to show to particular users, so I need to load the image of the colors that a particular user has, the color's information is contained in a ObjecDTO and one of the properties is its image path. My question is how should I store the image path in the database, are there any special rules, for example since the database and the image file are on the same server should I store a complete file URL, or a relative url, what characters should I escape etcetera, or are there better approaches than mine?

I've searched the web but mostly point to storing BLOBs in the database, which I've found to be a bad practice.

Any guidance is really appreciated.

regards Tristian.

like image 649
Tristian Avatar asked Dec 14 '10 21:12

Tristian


2 Answers

Create a column called: image_path VARCHAR(255) NOT NULL

And it would contain data like this:

~/images/image1.jpg

like image 75
Jack Marchetti Avatar answered Sep 29 '22 02:09

Jack Marchetti


I would really suggest to store relative paths and put the remaining as a config. That way, you can migrate more easily. For the datatype, I suggest something along the line of varchar(255) and ensure that restriction in your code.

And don’t forget to set it as UTF-8 if it’s a user field

like image 39
cslavoie Avatar answered Sep 29 '22 01:09

cslavoie