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

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

问题描述

我工作的一个Web应用程序,我决定(读这么多的岗位之后)的实际图像存储在文件系统和存储在数据库中的元数据。

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.

绝对:

优点:

很明显该文件甚至到其他应用程序读取DB

可以把照片的任何位置的驱动器上(将需要处理程序)

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)

缺点:

需要将absoulte路径转换为相对路径用于网站或创建一个处理程序

如果我迁移到另一台服务器上我可能不得不改变所有的路径

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.

我不能决定。

推荐答案

我会存储在数据库中的相对路径。这给了你最大的灵活性。加载图像是prepending一个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天全站免登陆