在文件系统中存储图像时,使用相对路径还是绝对路径? [英] When storing Images in the File System, Use relative paths or absolute paths?

查看:25
本文介绍了在文件系统中存储图像时,使用相对路径还是绝对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网络应用程序,我决定(在阅读了许多关于 SO 的帖子后)将实际图像存储在文件系统中并将元数据存储在数据库中.

I am working on a web app and I decided (after reading many post on SO) to store the actual images in the file system and to store the metadata in the DB.

我应该存储相对路径还是绝对路径.

Should I store a relative path or an absolute path.

我能想到每个选择的一些优势.

I can think of some advantages for each of the choices.

绝对:

优点:

对于读取数据库的其他应用程序来说,文件所在的位置很明显
可以将照片放在驱动器上的任何位置(需要处理程序)

It is obvious where the file is even to other apps reading the DB
Can put the photos anywhere on the drive (would require an handler)

缺点:

需要将绝对路径转换为相对路径以供站点使用或创建处理程序
如果我迁移到另一台服务器,我可能必须更改所有路径

Need to convert the absoulte path to a relative path for use in the site or create a handler
If I migrate to another server I may have to change all the paths

亲属:

优点:

只需将链接添加到 html 即可运行

Simply add the link to the html and it works

缺点:

如果我更改应用程序根目录,我必须移动图片或更改所有路径
必须将图片放在公共目录中(否则我在绝对路径上一无所获)

If I change the app root I have to move the pictures or change all the paths
Have to put the pictures in a public directory (Or I gain nothing over the absolute path)

好的,这些是我现在脑子里发生的一些事情.

Ok these are some of things going on in my head right now.

我无法决定.

推荐答案

我会在数据库中存储一个相对路径.这为您提供了最大的灵活性.加载图像是在前面添加一个IMAGE_ROOT"变量(它可能应该是可配置的)来获取文件系统路径的简单问题.这很重要,因为您可能想要移动存储图像的位置(例如,将它们放在更快的驱动器上).然后它只是更改可配置的 IMAGE_ROOT.

I would store a relative path in the database. This gives you the greatest flexibility. Loading images is a simple matter of prepending an "IMAGE_ROOT" variable (which should probably be configurable) to get the filesystem path. This is important because you might want to move where the images are stored (put them on a faster drive, for example). Then it is simply changing the configurable IMAGE_ROOT.

将图像的引用放入页面时,我会提供完整的 URL.同样,这只是将 URL_ROOT 添加到相对路径.如果您发现负载需要专门的服务器来提供图像,那么您可以轻松切换服务器.

When putting a reference to the image into a page, I'd put the full URL. Again, this is simply adding a URL_ROOT to the relative path. This gives you the advantage of being able to easily switch servers if you find load requires dedicated servers for serving images.

这篇关于在文件系统中存储图像时,使用相对路径还是绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆