Page 1 of 1

Appropriately rename uploaded file

Posted: Mon Jul 12, 2010 9:31 pm
by Rem
Hi there

I have a script that allows me to upload a file to my server. All this works great and all, but I was thinking on how to avoid collision with file names. For example two people upload an image with the same file name and extension which causes the newly uploaded one to overwrite the old one. I don't want that!

For my question, is there a function or something that will generate a unique string for a file?

I was also thinking of doing something like this to generate a unique file name:

Code: Select all

$file_name = "myimage.png";
$file_extension = "." . substr(strrchr($file_name,'.'),1);

$newFile = substr(time(),-7) . hash('adler32',$file_name) . $file_extension;
Is the above feasible? Should I suspect a collision any time soon?

Re: Appropriately rename uploaded file

Posted: Mon Jul 12, 2010 10:26 pm
by requinix
uniqid with the time as the prefix is generally more than adequate.