tags:

views:

161

answers:

9

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.

+3  A: 

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.

Bill the Lizard
That assumes he's already using a database and/or doesn't mind the overhead.
BryanH
@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.
Bill the Lizard
A: 

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.

Daniel S
instead of using a counter you could also use http://us.php.net/file_exists to check if the folder already exists.
Samuel
A: 

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.

John Boker
A: 

Could you use 4 characters as something descriptive as to what is contained within and the last 3 as an incremental number?

rvause
+1  A: 

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.

André Hoffmann
+1  A: 

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 )
?>
BryanH
+1  A: 

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 :)

A: 

"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));
DigitalRoss
+2  A: 

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

Or just sha1(microtime())

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