Web Hosting Talk







View Full Version : Random File Name


MikeA
04-16-2003, 02:03 PM
Can someone tell me or give me a quick snippet of code to assign a random filename. I'm trying to create key files.

A name like a1dhj53hj35hjjk3lkj if this is possible.

BTW, I need it for PHP.

Thanks a ton.

MikeA
04-16-2003, 02:34 PM
Ok, whipped out the PHP 4 Bible and found a script that I changed to do what I need. Here is what it is:


<?PHP

mt_srand((double)microtime() * 1000000);
$charset = "abcdefghijklmnopqrstuvwxyz1234567890";

for ($x = 0; $x < 20; $x++) $key_name .= random_char($charset);

$key_name = "$key_name.html";

print("$key_name");

function random_char($string)
{
$length = strlen($string);
$position = mt_rand(0, $length -1);
return($string[$position]);
}

?>


Here is what the output will look like -> g1hbpihfh8y8tfwb006r.html

ilyash
04-17-2003, 09:14 AM
look here http://www.webhostingtalk.com/showthread.php?s=&threadid=133222