How do I store the location of an image in a database?
Solution 1
Create a column called: image_path VARCHAR(255) NOT NULL
And it would contain data like this:
~/images/image1.jpg
Solution 2
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
Comments
-
Tristian about 2 years
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.
-
Tristian over 13 yearsHi, yes I was thinking the same, could you give an example of a relative path?.
-
Tristian over 13 yearsYes, I've searched, and even tried different search terms but it returned the same answers.
-
Tristian over 13 yearsAlso, what precautions should I take with the characters?
-
suhprano over 13 yearsI think he just means a path referencing the data, whether it be in the file system itself, or a ftp/url of some sort.