10

I'm having a debate with a fellow programmer (PHP) and we both disagree when it comes to GUIDs. Assuming that information about each image is being stored in a DB and has its own primary key (int).

What reasons could there be for using a unique ID for the image filename, beyond not having to worry about duplicate filenames?

I don't want to disregard his methodology, but it doesn't sit well with me either.

Thanks! Ben

Update: Having heard many +1's for GUIDs, how might SEO be affected by "randomly" generated image filenames? (Thanks Sukumar)

Ben
  • 251
  • 1
  • 5
  • 13
  • Regarding SEO see http://www.seroundtable.com/archives/007366.html In this case, I don't think primary key vs GUID makes any difference. – Sukumar Aug 08 '11 at 13:41

4 Answers4

14

What reasons could there be for using a unique ID for the image filename, beyond not having to worry about duplicate filenames?

Using an auto-increment primary key for a file name would make it possible to guess other images' URL's, which may be something you do not want - depending on how your system works, users could, for example, gain access to images that aren't intended for publication (yet).

I think using unique IDs is quite a good idea.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
4

Advantages of using GUID for filenames

  • Get unpredictable filenames.
  • Names that scale (don't need to worry about renaming files when DB is sharded).

Disadvantages of using GUID for filenames

  • Have to store additional data in the database.

Advantages of using DB primary key for filenames

  • Saves storage by reducing the need for an additional column.

Disadvantages of using DB primary key for filenames

  • Creates predictable filenames allowing users to "guess".

Summary: It is a trade-off you need to consider based on your requirements. More preference seems to be given to choosing GUIDs though.

Sukumar
  • 3,502
  • 3
  • 26
  • 29
  • Thanks Sukumar, very clear list. GUIDs have a lot more punch than I gave them credit for. Would an SEO consideration be better as a separate question? – Ben Aug 08 '11 at 09:00
  • SEO consideration could perhaps be an additional point in the answer. You could clarify your question further and improve this answer as well, making it more complete. – Sukumar Aug 08 '11 at 09:03
1

UUID is great when you need to have unpredictable filenames, so no one could download all the files without having the right urls.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • This is not entirely correct: http://stackoverflow.com/questions/4517497/how-secure-are-guids-in-terms-of-predictability – OverZealous Aug 08 '11 at 08:46
  • @OverZealous: oh, my fault. Did not know that. So it is unpredictable for about 99% of people )) Btw, any proofs that GUID is actually predictable? – zerkms Aug 08 '11 at 08:47
  • Sorry, I rewrote my comment. I didn't mean to come across so harsh! – OverZealous Aug 08 '11 at 08:48
  • @OverZealous: it wasn't at all. I admit I don't know a lot of things and becoming smarter is my aim of being here ;-) – zerkms Aug 08 '11 at 08:50
  • @OverZealous: According to wikipedia "Cryptanalysis of the WinAPI GUID generator shows that, since the sequence of V4 GUIDs is pseudo-random, given full knowledge of the internal state, it is possible to predict previous and subsequent values". So perhaps it is not good enough for just small amount of companies/projects – zerkms Aug 08 '11 at 08:52
  • Thanks zerkms, 99% unpredictability is more than sufficient for our purposes :) – Ben Aug 08 '11 at 08:58
  • @Ben: you need to know that I've taken number 99 from nowhere, it is just a way to say "really a lot". I think the real value is much closer to 100 though. ;-) – zerkms Aug 08 '11 at 08:59
0

If you store it in a database using a primary key I would suggest using that as unique ID (and thus filename). It doesn't make much sense to have two unique columns in a database identifying one resource. I wouldn't use an auto-increment field for this primary key by the way. It's to easy to guess other image ids which may be unwanted.

The choice between the two is a matter of taste I think. We use both. The filenames are what is mostly used but then you'd have to have rights to view the file. We use a unique ID as well but thats to avoid rights checking and enabling someone to share images to non-users.

hoppa
  • 3,011
  • 18
  • 21