Unique Random Download Link Generator for uploaded files.

Hi, I am new to this website and also to php. I am programing in php and MySQL from past 2 months and strive to be a better programmer.

I have created a website on my computer and it is meant to be a online file uploader.
The basic idea is that the users are able to upload various files on my website, after uploading,my site gives back the url as to where it is stored.

This approach is fine if the names of the files are different.
but then when it comes to two users trying to upload two different files but having same name and extension then there is a problem.

So to fix this there should be a unique random prefix and then the file name. This random prefix should not be used again.

I am now stuck as to how to generate this random number prefix.

Hi there,

Try setting the uploaded files to this:
[php]
$newfilename = substr(md5(time().rand(0,100000)),rand(2,10),6)."_".$originalfilename;
[/php]

It might be a tad overkill - but you can be fairly certain that you won’t get a duplicate :smiley:

You could of course just use “time()” (which outputs the current UNIX timestamp).

Let me know if this helps or not!

@Smokey Thanks for replying.
I used this [php]function uuid()
{
// version 4 UUID
return sprintf(
‘%08x-%04x-%04x-%02x%02x-%012x’,
mt_rand(),
mt_rand(0, 65535),
bindec(substr_replace(
sprintf(’%016b’, mt_rand(0, 65535)), ‘0100’, 11, 4)
),
bindec(substr_replace(sprintf(’%08b’, mt_rand(0, 255)), ‘01’, 5, 2)),
mt_rand(0, 255),
mt_rand()
);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service