Something like this could work:
Code: Select all
function createRandom($length=8)
{
$allowed = 'abcdefghijklmnopqrstuvwxyz1234567890';
for($i=0;$i<$length;$i++)
{
$ret_val .= $allowed[rand(0,strlen($allowed))];
}
return $ret_val;
}
However, there's no guarantee that the file won't already exist. You could add the time with microseconds on that to reduce the chances significantly, but the only way to
guarantee each random string is also unique, you'll need to do a check - either against the filesystem or a database of all files, to check that the random string doesn't already exist.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.