Thanks for all your help - see blow original question, and my edit following the two line rules (I cannot yet answer my own questions as a new user..).
I've looked around and question (here) almost meets the aims of my question:
For example, I'm creating a user data directory for each user on a web application; the folder of-course must be unique but also abstract for security (using their user id for example, would not be appropriate.
So far I've created the following function; it generates a unique folder name, checks to make sure it doesn't already exist and assigns it to a variable. It then loops back if the dir already exists:
function generate_unique_userDirectory(){
$userDirectory = md5(uniqid($uid)); //Generate a unique folder name.
if (is_dir($userDirectory)) {
return FALSE; //If the dir exists, report so
} else {
return $userDirectory; //Return unique foldername
}
}
While loop is used to keep going until an unused folder name is found.
while (!$userDirectory = generate_unique_userDirectory()) {
echo 'folder exists...loop back try another';
//Try another:
$userDirectory = generate_unique_userDirectory();
}
Is there a better way of doing this, my main concern is whether I'm over-complicating the procedure?
Many thanks for your time.
My findings thanks to all that contributed!
Thanks @Veger, and everyone else; your assistance was brilliant; I've since re-worked the function based on your advice:
function generate_unique_userDirectory($uid){
$userDirectory = md5(uniqid($uid));
while (is_dir(BASE_URI . "$userDirectory")){
$userDirectory = md5(uniqid($uid));
}
return $userDirectory;
}//End of generate_unique_userDirectory funcation decleration.
//Example call to function:
$userDirectory = generate_unique_userDirectory($uid);
echo "The generated user directory is: $userDirectory";
As suggested, I've put the folder name generation while loop within the function which now makes the function call much simpler.
In response to your second bullet point @Veger, it is my understanding that as I've fed the 'uniqid' function to the md5 function this will result in a new string each time (though I may have misunderstood).
The purpose of passing the $uid to generate_unique_userDirectory()
is to further 'salt' the generated string, however, I may be taking it a step too far!
Many thanks to all- a great first time on stackoverflow...