I've got a table having a primary key that's auto increment. I wish to come with an image connected using the primary key but I'm not going the main answer to be revealed. Would naming the pictures something similar to:

$filename = md5($primarykey + $secret_string) . '.jpg';

be a great choice?

I'm worried that there might be an accident along with a file be over written.

Another use of course is to develop a random string, check it does not exist like a file and store it within the database... but id prefer to not store additional data if it is unnecessary.

Another choice is may well transformation youtube url style e.g 1=a coupleEqualsw however with a randomised order e.g 1=x 2=m... however there's the risk of it being decoded... plus md5 would most likely be lighter than any youtube url function.

I'd guess I'm coping with over 2 million records what exactly may be the probability of the collision? Which option can you pick or are you able to think about a much better approach?

Make use of a linear congruential generator. When you purchase the values correctly, then you'll have a pseudorandom sequence having a large period. No collisions, but observe that case an obfuscation method and will not provide any real security (however i assume that's not what you're searching for).

The Code you found here is suitable for the job. It's not necessary to be worried about collisions, no integer that the databases uses can lead to exactly the same md5 hash every other.

If you wish to be really sure: this is a litte test script )

<?php
for($i = 0; $i < 1000000; ++$i) {
    $hash = md5($i);
    if(isset($x[$hash])) { die("COLLISION!"); }
    $x[$hash] = true;
}        }
echo "All is well";

A far more complex solution appears like a total waste of your time and effort but just in case you would like shorter Ids you can provide a short random produced id to each picture (and appearance whether it's alrealy been utilized on generation some time and generate a different one if thats the situation)

I'd guess I'm coping with over 2 million records what exactly may be the probability of the collision?

Based on Wikipedia, you will need a lot more than 2*10^19 records to obtain a 50% probability to possess a minumum of one collision, so I'd say it's not necessary to worry.

Only use a hash from the primary key. The likelihood of there as being a collision is veeeery low.

If you wish to make use of the ID, but don't wish to reveal the ID, and wish a minimal chance of collisions one option is to utilize a hash from the ID. The hash could be consistent, irreversible, and (excepting huge datasets - 232ant with respect to the hash used) unique for every photo. You appear to achieve the fundamental concept of it inside your question:

$filename = md5($primarykey + $secret_string) . '.jpg';

You substitute MD5 for any hashing better formula of your liking. Ideally something having a bigger output. According to skimming a number of articles in reaction towards the comments, it might appear that SHA512 or something like that similar will be a better fit.