Page 1 of 1

Random String -- Never the same though...

Posted: Mon Jun 23, 2008 11:46 pm
by tecktalkcm0391
Isn't MD5 suppose to never generate the same string, because can't I do:

Code: Select all

md5(time());
To get a unique string?

Any other ideas... cause they can't be the same... its for storing images like myspace and facebook do...

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 12:23 am
by alex.barylski
MD5 should (in theory) generate a new string each time the script is invoked, however if you are calling:

md5(time());
md5(time());

One after the other it might not generate a unique string each time.

Maybe try uniqueid:

http://ca.php.net/manual/en/function.uniqid.php

Most randomizers I've seen are just a simple loop which generates a random string of 32 characters or 12 or howevere many you need.

Setup a for loop whihc counts to 12 or 25 or whatever and use the function mt_rand

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 1:19 am
by Mordred
Properly using random is a hard task (as you've seen yourself). Do describe in more detail why you need the random value, as different "random strings" will have different properties, which you might or might not need - entropy, uniqueness, predictability, distribution.

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 7:34 am
by tecktalkcm0391
I need to generate filename string to store images that are uploaded. Each one gets a unique random filename, just like when you upload an image to MySpace or Facebook, your image filename comes out to something like n2d8d3jd82m8fl28jfl9.jpg or l_28nf84mjf84mf8m4ifml4f.jpg

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 8:41 am
by freeformer
Is there any metadata for the images stored in a database?

If so, why not just use the primary key value from the database as the filename?

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 9:24 am
by Kieran Huggins
store them as their own MD5() - that way if collisions happen, it's the same file content anyway.

http://www.php.net/manual/en/function.hash-file.php

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 8:54 pm
by LBmtb
Kieran has a good idea. What if there are two files named the same thing though - that would generate the same hash but the contents would be different. 'group.jpg', 'me.jpg', etc are just some common names for images.

Maybe a hash of a string that contains both the name of the file and the time?

Re: Random String -- Never the same though...

Posted: Tue Jun 24, 2008 10:39 pm
by Kieran Huggins
hash_file creates a hash from the contents of the file, not the filename. If you ever get a collision, it would be because the images are identical. filenames be damned!

Re: Random String -- Never the same though...

Posted: Wed Jun 25, 2008 9:13 am
by Mordred
Careful:
1. Two people upload the same avatar, same md5
2. One of them changed his avatar, you delete the old file (gotta check first!)
3. The second loses his avatar

Re: Random String -- Never the same though...

Posted: Wed Jun 25, 2008 10:01 am
by Kieran Huggins
delete the file? why? ;-)

You could run a "housekeeping" cron job to delete files that no longer exist in the DB, but given that storage space is so cheap (and getting cheaper) I wouldn't even bother.

A good thing to keep in mind though!

Re: Random String -- Never the same though...

Posted: Wed Jun 25, 2008 4:26 pm
by tecktalkcm0391
This is what I'm planning on doing... I'm going to have pictures saved in sub folders, by some date type (a combination of month, week, day, or year) and then have a random 2 digit number for a sub folder on that, then a MD5 of the file, which is checked against the database to make sure it doesn't override any image already.

Does that sound like a good plan, because then at least if the same file is uploaded, it may be in a different folder (.../##/) from the random number, and then they have to be uploaded on the same day for it to even have that chance...?

Re: Random String -- Never the same though...

Posted: Wed Jun 25, 2008 4:42 pm
by Kieran Huggins
sounds over-complicated - just don't delete them and lose all that extra date junk.

Re: Random String -- Never the same though...

Posted: Wed Jun 25, 2008 5:13 pm
by tecktalkcm0391
sounds good, but how many files can be created using md5 cause it has to be limited to a certain number of random strings... and i'm expecting thousands of files, so i guess just do a buy year folder, so that it doesn't run into any errors down the road...

edit: nevermind i don' think i'll have 16^32 files as md5 can produce

Re: Random String -- Never the same though...

Posted: Tue Jul 01, 2008 12:42 am
by Ollie Saunders
Careful:
1. Two people upload the same avatar, same md5
2. One of them changed his avatar, you delete the old file (gotta check first!)
3. The second loses his avatar
Great catch. You could add the user id to the hash data to solve that. But personally I'd just use uniqid() and be done with it.