1

What's the advantage of storing images or the path to images in a database compared to directly linking to the images from your script?

Edit: Isn't hardcoding the urls in the script also faster since you don't have to do a database lookup for every image in your webpage?

Tiddo
  • 6,331
  • 6
  • 52
  • 85
  • possible duplicate of [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – Marc B Sep 20 '11 at 18:48

3 Answers3

1

Because you can dynamically alter the paths later, or be able to manipulate them, otherwise your 'script' would have to be updated EVERYWHERE (imagine your script(s) grow to large sizes).

Database makes management of data easier, and eliminates hard coding in your example in scripts.

It is never good to hard-code something.

EDIT

I just noticed you said 'storing image' I wouldn't store images in the DB, safe them for the files system and reference with the path like you stated in your question.

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • But usually when you only change the image in a webpage, you could as well replace the image in the filesystem. If you don't only change the image, you'd probably have to edit the source anyway. – Tiddo Sep 20 '11 at 18:56
  • You said that using a database you could alter the path to an image later. I think there are 2 reasons why you might want to do this: you just want to change the image, or you're giving your website some kind of makeover. In the first case you could as well replace the original image with the new one (that's as easy as changing the url in the database), and in the second case you already have to edit the source code, so you could as well edit hard coded urls with it. Probably I'm still missing something here, I 'feel' that storing urls in a database is better, but I still don't understand why. – Tiddo Sep 20 '11 at 19:16
  • @Tiddo, you are thinking on small scale, I am trying to explain in large scale storage if your site grows. If you are using a db to store 3-5 image urls, then yes, its pointless, HOWEVER if you are going to use 100's or 10,000's of images, it makes great perfect sense to manage/store in such a fashion. – Jakub Sep 20 '11 at 19:21
  • Ok thanks! That's probably why I couldn't see the advantage: I have absolutely no experience at all with large scale websites. So probably it's pointless for the websites I'm building. – Tiddo Sep 20 '11 at 19:50
  • Its not pointless, its good practice and the proper way of doing it. – Jakub Sep 23 '11 at 02:58
1

It's impossible to answer to such a vague question.

What images you're talking about? Design images? photo gallery images? avatar images? It's all different cases each with own solution. Storing image names in the database will do any good for only one case out of these three, as it would be easier to group, arrange and interlink images in the gallery. While for the other cases there is not a single reason to store image names in the database.

Anyway, it's all applicable to the image names only. As there are not a single reason to store any URL or path beside image name. Url should be computational based on some rules, not hardcoded one.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I don't think it's a vague question. I don't ask if it's better to store images in a database, I'm only asking for advantages. All of the answers to this question, Including your own, answered my question in one or more ways correctly, so apparently my question is clear enough. – Tiddo Sep 20 '11 at 19:52
  • asking for advantages without practical question will lead you to fault. Definitely. – Your Common Sense Sep 20 '11 at 20:03
  • I agree with you. But this wasn't for any practical application at the moment. I've read multiple times about this, and I was just wondering what the advantages were. – Tiddo Sep 20 '11 at 20:20
0

I do not typically do this with basic site images, but the definite advantage can be for scalability purposes. If the image is going to show up in different scripts, they can all reference to the db, thus giving you the ability to only have to change the url in one place.

JamieHoward
  • 693
  • 2
  • 11
  • 27
  • But in that case couldn't you replace an image in the file system as well? Than you'll also only have to change one thing. – Tiddo Sep 20 '11 at 18:55
  • 1
    But of course, it would have to be named the same thing. If you had a SQL row `LogoImage` that was equal to "default_logo.png", and you wanted to change it for Christmas, like Google does for instance. Instead of deleting or overwriting your current logo (named the same thing), you can just change `LogoImage` to "christmas_logo.png" – JamieHoward Sep 20 '11 at 19:03