views:

383

answers:

5

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.

Absolute:

Pros:

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)

Cons:

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

Relative:

Pros:

Simply add the link to the html and it works

Cons:

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.

I can't decide.

+4  A: 

If you are interested in the portability of the info in your database, just store a base path in the database as well. That way, when you move the files, all you need to do is modify the base path that you've stored in the DB. This information should be stored separately from the file paths. Storing such information in the same row would create a lot of unnecessary duplication.

vezult
Would you recommend storing both for each image or just one base path in a settings table
Sruly
To "complete" the answer, store relative paths in the database and store the base path also (not necessarily in the same table, though). +1
Cerebrus
+3  A: 

I prefer to store them as relative paths so the application is not dependent on its location. With ASP.NET there is the "~" to automatically signify the application root, you could do the same and then simply replace it with a constant when using it, that way you do not have to worry if you change the app root.

Rob West
+4  A: 

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.

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.

Travis Jensen
+1  A: 

If you're using SQL Server 2008, you can solve this problem neatly with the new FILESTREAM data type.

Jacob
A: 

I concur. Storing relative paths means less info in the db and if you store a base path somewhere, you only need to modify it once. You then build your full urls by adding the shorter link from the database to your basepath.

evilpenguin
You concur with what? ;-)
Cerebrus
Sorry, underestimated the speed the new posts appear. I was referring to what vezult said.
evilpenguin
Things aren't necessarily sorted by date on Stack Overflow. Generally, they're sorted by votes. So you shouldn't depend on what order they're in since people can move things up or down on a whim.
Chuck