views:

194

answers:

4

If I make the filename of each uploaded file PHPs microtime(), are the risks of a collision realistic? And is there a way to further randomize the filenames?

I'm expecting about 20,000 uploads in about a week. That's 2800/day or 119/hour (assuming even distribution). Does anyone have any experience with assuming microtime to be unique?

Thanks!

+4  A: 

The best way to generate random file names in your case is to use GUIDs. They are great because this algorithm guarantees uniqness of the value even for multiple server. If you are using windows the com_create_guid function is for you. Also you can look at uniqid function which available for all PHP platforms.

Ivan Nevostruev
There are some excellent examples in the comments for uniqid that provide more details and other examples for uniqueness.
Goyuix
A: 

Use uiqid()

powtac
+1  A: 

One common method is to create directories in the format of YYYY/MM/DD/id.jpg. That way you don't run into file system limitations as well.

Jeff Ober
Those limitations being files/directory? What are those on an apache linux box?
Alex Mcp
A: 

I think this is not a huuuuge problem. (kinda paranoid)

Just make a check if exists, if exists, solve the colision (maybe in a while loop until guarantee its unique).

I can't imagine so many file per micro-second.

Ismael