I want to use PHPs uniqid()
function to create a filename for image upload.
There will be only one person uploading images and they can only upload one at a time. I don't care that the filename can be reverse engineered to get the time because it's just a filename.
Is uniqid 100% unique for my use case?
Try to use something like below:
md5(rand(1,1000000).uniqid(mt_rand(), true))
This code piece had already increased uniqueness.
You can use time() function in this case
$uniqueFilename=time().uniqid(rand());
Or like this one
$originalFileName = myimage.png
$uniqueFilename=time().$originalFileName;
Yes, uniqid()
will always be unique in your case because you allow only one person and one upload at the same time. This will work because uniqid()
is generated based on the system time.
You could also use uniqid(rand(), true)
to generate a more random filename or md5();
to encrypt your filename.