Any better way than time()

Soldato
Joined
2 May 2004
Posts
19,950
Hi,

Currently, infront of the name of files uploaded onto my site I put a time stamp (time()). Sometimes the time stamp is up to 10 digits out compared to the link meaning sometimes they don't work and I have to change the name of the file to make it work.

Is there anything better than time() I can use that will avoid this problem please?

[Edit]
Also, I put a time stamp in to avoid people uploading a file named the same as another file, as this would overwrite the other file.

Thanks
Craig.
 
You can create more random strings than simply using time, e.g. combining uniqid()/rand() etc. However, collisions don't seem to be your issue. When you say the 'link' is not the same as the timestamp, how are you generating the link and the timestamp?

You should be storing the unique value you're creating (using time() in your case) in a single variable and use this to create the link and the filename. If you run time() once, do some processing, and then run time() again, the second value of time() will be larger than the first because of the time taken to do the processing!

When I started this post the time was 7:40, and when I finish writing it will be 7:43 ;).
 
True, but most of the time it works, just sometimes the link is a few out for some reason. :(
I'll check out rand and uniqeid and see if I like them :)

Thanks
Craig
 
I think what people do commonly is hash the file and then add the filename to the end, to make it "look funky-but-recognisable" to Joe Bloggs - and of course provide a 0.000000000000000000000000000000001% collision chance :)

PS - this is what I would do, or if the filename was too long I'd truncate it and add it to the end :)
 
OK, I noticed that on the upload part i was doing time() . before the filename and then on the SQL part I was doing time() . before the filename meaning sometimes they might have been slightly out... which they were. So instead I did this:

$time = time();

And then instead of using time() before the filename I used $time :)

It hasn't gone wrong since :)

Thanks for the help everyone
Craig
 
You may want to take a little more advice from this thread as the method you are using will cause problems when two people do it @ the exact same time.

Try adding something unique to the name, like a hash of the users ip or something.
 
Was thinking that, that md5 uniqid time thingy is pretty good but it's a little long, makes the filename real long :(

Could I use rand() on it's own? Would there be any down sides to that?

Thanks
Craig.
 
Back
Top Bottom