3

HI

Whats the best way to create a random name for a folder?

Its going to be used for a folder name to store documents. But lots of folders are going to be created and so need it to be unique each time if possible.

Length should probably be around 7 characters.

1
  • Any preference as to how long it should be? What is this going to be used for? More info please. Commented Sep 28, 2009 at 17:27

10 Answers 10

13

You can also try PHP's uniqid(), with the param more_entropy set to true.

Or just sha1(microtime())

Sign up to request clarification or add additional context in comments.

3 Comments

This is the only sensible answer here. +1.
Isn't microtime() only available on *NIX? us3.php.net/manual/en/function.microtime.php
No, it's just called an UNIX timestamp, but available on all systems.
5

If it needs to be unique, then I would forget about it being random and just increment a counter. If you need to associate the contents of the folders with records in a database, all the better. You can just have an autoincrement column in your database and use it as part of the folder name.

2 Comments

That assumes he's already using a database and/or doesn't mind the overhead.
@BraynH: That's why I said "if". You can still implement a counter in PHP, it's just more convenient if you're associating the contents with a database record anyway.
3

Here's the function I use:

function makeRandomString($max=6) {
    $i = 0; //Reset the counter.
    $possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $keys_length = strlen($possible_keys);
    $str = ""; //Let's declare the string, to add later.
    while($i<$max) {
        $rand = mt_rand(1,$keys_length-1);
        $str.= $possible_keys[$rand];
        $i++;
    }
    return $str;
}

EDIT: As Bill the Lizard said, you'd better add some kind of counter. Although very unlikely, it is possible that the same string can be created twice.

1 Comment

instead of using a counter you could also use us.php.net/file_exists to check if the folder already exists.
2

Is 7 char folder name a hard and fast rule (what is the cause of the limit)?

I use YYYYmmDDHHMMSS (and add millisecond if you find yourself getting many collisions).

Where

  • YYYY = four digit date
  • mm = two digit month
  • DD = two digit day
  • HH = two digit (24hr) hour
  • MM = two digit minute
  • SS = two digit second

You may also want to test to see if the directory has already been created and then sleep for a random number of milliseconds before trying again.

PHP Code:

<?php
$dirName = date( 'YmdHis', time() );
@mkdir( $dirName )
?>

Comments

1

Do you really need it to be random?

If you need it to be unique I'd suggest creating a counter and converting the integer number to hex or even better base 36(26 chars + 10 digits). After creating the folder increase the counter and you're good to go.

Comments

1

php has a standard function, tmpnam that does exactly that; it creates a unique file name (actually creates the file, but can delete and use as folder later :)

Comments

0

with the requirement that it be unique and 7 characters i'd probably use a number encoded to base 36 (0-9 and a-z), that way you can just see what the last folder was and incrememnt it to create a new one.

Comments

0

"rh" . mt_rand(10000,99999)

Then check to see if it's unique and regenerate if it isn't. In a way this is better than saving state, like a counter, because less can go wrong.

do {
  $f = "rh" . mt_rand(10000,99999);
} while (file_exists($f));

Comments

0

I think best approach to naming is using uniqid() function.

$folder_name = uniqid();// eg: 4b3403665fea6

or with a static prefix:

$folder_name = uniqid('folder_'));// eg: folder_537834f6e981a

or with today (date) prefix:

$folder_name = uniqid(date('h-i-s')._); //eg: 04-23-59_537835dff2017

Comments

0

In this example we can look a random key with min and max length.

substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.