0

EDIT: I need to generate a string of 7 chars that is based on the id of the row. So knowing the id of the image and a secret key, i should get the generated string.

the string must contain chars from "a" to "z" and numbers from 0 to 9.

I have a dir that contains photos like this

dir/p3/i2/s21/thumb.jpg

the generated string is p3i2s21, then is used to calculate the path of the image.

EDIT: currently im using the id of the image: id = 55 then i modify and i get path = 000000055 then path = "000/000/055" then path = "000/000/055/thumb.jpg"

ready to use!

now i want something more clever because is easy to track down all the images from a server, because ids are sequencial: 1, 2, 3, 4, 5, 6...

so i must think of creating from 55 a string that is 7 char length and will not overlap with other numbers. I can even transform the 55 to 0000055 and from this convert to a 7 char length string using a secret string. then when i got the secret string and the id i want to get back that 7 char length string.

Is this possible? i was thinking about hases but they only uses 0-9 and a-e and are more chars.. :s

Totty.js
  • 15,563
  • 31
  • 103
  • 175

2 Answers2

1

I think you would need to use a database to do this. You could generate random 7 char strings and store them in a table along with the path they are mapped to.

Aside from that, there's really no easy way you could reliably take an arbitrary path, "compress" it down to 7 characters, and then be able to get the path back again from those seven characters.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

I would add a new column to your table to store a GUID value. This way, you have an identifier that's non-sequential.

If you can't use a GUID and really it need it 7 characters long ... well, what I would do is generate the GUID anyway, take the (first/last) 7 characters, make sure it's unique, and save it. If it wasn't unique, I'd generate another GUID until it was.

This is very similar to Eric's answer by the way; I just wanted to mention GUIDs specifically.

overslacked
  • 4,127
  • 24
  • 28
  • yes, of course will add another column in the table, and of course im using a database, the only problem is to get that 7char string... I will check it out the GUID – Totty.js May 12 '10 at 19:57
  • @Totty - if you tell us what database system you're using, I'm sure we could provide you with a function to generate a unique/random 7 character string. – overslacked May 12 '10 at 20:21