下面的这个算法保证你的文件名不会重复了function my_setfilename(){
$gettime = explode(' ',microtime());
$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
$rand = '';
for ($x=0;$x<12;$x++)
$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
return date("ymdHis").substr($gettime[0],2,6).$rand;
}
$gettime = explode(' ',microtime());
$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
$rand = '';
for ($x=0;$x<12;$x++)
$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
return date("ymdHis").substr($gettime[0],2,6).$rand;
}
function getSaveName()//获取不重复的保存文件名
{
$d = gettimeofday();
return $d['sec'] .$d['usec'];
}for($i=0;$i<30;$i++)
{
echo getSaveName() . "<br>";
}输出如下:
1237253939203129
1237253939203312
1237253939203439
1237253939203565
1237253939203690
1237253939203815
1237253939203950
1237253939204075
1237253939204200
1237253939204325
1237253939204450
1237253939204575
1237253939204701
1237253939204826
1237253939204951
1237253939205078
1237253939205204
1237253939205329
1237253939205454
1237253939205579
1237253939205705
1237253939205830
1237253939206864
1237253939207007
1237253939207132
1237253939207257
1237253939207381
1237253939207506
1237253939207630
1237253939207755?>
<?php
echo uniqid() . "\n";
// no prefix
// works only in PHP 5 and later versions
$token = md5(uniqid());
echo $token . "\n";
// better, difficult to guess
$better_token = md5(uniqid(rand(), true));
echo $better_token;
?> <?php
/**
* Simple function to replicate PHP 5 behaviour
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}$time_start = microtime_float();// Sleep for a while
usleep(100);$time_end = microtime_float();
$time = $time_end - $time_start;echo "Did nothing in $time seconds\n";
?>
<?php
echo microtime(true);
?>