Random String -- Never the same though...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Random String -- Never the same though...

Post 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...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

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

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post 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.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

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

Post 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
User avatar
freeformer
Forum Newbie
Posts: 14
Joined: Tue May 13, 2008 1:54 pm
Location: UK

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

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post 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
LBmtb
Forum Newbie
Posts: 23
Joined: Wed May 14, 2008 11:14 am

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

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post 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!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post 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!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

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

Post 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...?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post by Kieran Huggins »

sounds over-complicated - just don't delete them and lose all that extra date junk.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

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

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

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

Post 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.
Post Reply