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

泪湿孤枕 提交于 2021-02-07 13:24:06

问题


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.


回答1:


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

And it would contain data like this:

~/images/image1.jpg




回答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




回答3:


This is really been asked a lot:

store image in database or in a system file?



来源:https://stackoverflow.com/questions/4444442/how-do-i-store-the-location-of-an-image-in-a-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!