Ok, well ive been playing with file uploads recently and have come across the need to create filenames on the fly. Following is a random filename generator. Its actually pretty simple when you look at it...
/** Function: generate_rand_filename
* Param1: $1 - Generates a random string thats as long as param $l given.
**/
function generate_rand_filename($l)
{
//Allowed characters//
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//Seed the random number generator//
srand((double)microtime()*1000000);
//Loop upto $l times and add a character to the string each time//
for($i=0; $i<$l; $i++)
$rand.= $c[rand()%strlen($c)];
//Return your new string//
return $rand;
}
Thats the basis of the function. But thats not all. You will also need to add an extension to the filename, and may also want to have a prefix. If you do, just use the following code:
$type = '.jpg';
$prefix = 'upload-';
$filename = $prefix;
$filename .= generate_rand(10);
$filename .= $type;
Good luck...
Posted by OLLIE at 13:01pm
No comments yet. Be the first to add one!