Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6235143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:37:08+00:00 2026-05-24T10:37:08+00:00

Thanks for all your help – see blow original question, and my edit following

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:37:09+00:00Added an answer on May 24, 2026 at 10:37 am

    Some of my thoughts on your design:

    • As Wesley already commented generate_unique_userDirectory() suggests that the returned directory is unique. So you should not need to check after calling whether its result is a unique directory or not…
    • Calling the function a second time results in the same failure, as $uid is not changed for the new user.
    • If it does change, why not put your while loop inside the function? So it always returns a unique directory.
    • By calling the function in the while-statement and in the loop itself, results in it being called twice after failure. First call in the loop which is overridden by the call in the statement.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for all your help so far I have made the following changes This
Thanks for your help! I'd like to output all companyName entries that have uploads
A bit confusing this one but should make sense. Thanks to all your help
First, thanks for all your help! You really make a difference, and I GREATLY
Thanks for all your help, as usual, i am looking for some more information...
Thanks at all for all your help for now.I have another little issues This
UPDATE: Thanks for all your help guys! I just need to take a litte
Thanks for all your help, I now have add, update, and delete functions on
I am a newbie to VBS scripting. Thanks for all your comments! I fixed
first of all thanks for taking your time! I'm a junior Dev, working with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.