开发者

Linking database id's and files

Say I'm using MySQL and I have a column id which is my primary key in some table. In PHP I generate a random string/integer and store that as the id in my database. I then refer开发者_开发技巧ence that id as the name for my file everywhere in my code (as I know it will always be unique).

My question is, should I add an extra column in the table to store the full location of said file, or is referencing it by the id everywhere fine?

I ask this because my application is still in the early stages of development and I may end up having to change my schema and/or databases depending on its usage.

===EDIT===

Let me clarify what I'm asking for further. I am storing the id of some image in my database. Right now I know all the image names are the same as the id and that they are stored in say /123img45. Since I know their location right now, I don't bother storing the path to the file, but later in development I may change databases or change the name of the folder etc...

Is it worth storing the path? Or is it better to cross that bridge when you get to it


I wouldn't bother. You can keep the path to the folder where files are stored as a configuration variable in your application code. This also means you can move the folder around without needing to touch the database.

If you need to store the full path in the future, it's easy to update the database to add the full path later if you find you need to for whatever reason.


You can store only the id of the file if your files are always in a known path or in a set or known paths and can decide with path contains each id. The id must be a suitable file name (no '\', '/' and other characters) and must be unique (ensure id generation could not generate same id after some time as collision will happen).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜