一、适用场景:
无法使用从数据库中返回的自增长数字,给上传图片重命名。
这是图片或文件上传的流程决定的。
一般图片上传处理过程是,先上传图片到服务器,重命名之后,插入到数据库。
也就是说,在数据库中非常容易获得的自增长id,无法用于给上传的图片重命名,来避免文件名称的重复,
而采用从数据库中获取最大id加1的方式,增加了数据库连接的次数,不适用于高并发和数据量巨大的情况;
二、常规方案:
1,guid:32 字符十六进制数。
格式:GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每个 x 是 0-9 或 a-f 范围内的一个32位十六进制数。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即为有效的 GUID 值。
优点:几乎不会重复;
缺点:对于给上传的图片重命名,还是过长了。
用法:
复制代码 代码如下:
/*
com_create_guid()是php5版本支持的功能,对于不支持的版本,可以自己进行定义;
*/
function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
echo(mt_rand());
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}
复制代码 代码如下:
$str = "Hello";
echo md5($str);
?>
复制代码 代码如下:
/*
*结合time()函数使用,以1970年到当前时间的秒数作为种子数。
*/
$str=time();
echo md5($str);
?>
复制代码 代码如下:
var_dump(uniqid());
var_dump(uniqid("a"));
?>
复制代码 代码如下:
/*
* 参数 suffix_len指定 生成的 ID 值附加多少位随机数,默认值为 3。
* 感谢“Ivan Tan|谭俊青 DrinChing (at) Gmail.com”提供的算法。
* @param int suffix_len
* @return string
*/
function fast_uuid($suffix_len=3){
//! 计算种子数的开始时间
$being_timestamp = strtotime('2013-3-21');
$time = explode(' ', microtime());
$id = ($time[1] - $being_timestamp) . sprintf('%06u', substr($time[0], 2, 6));
if ($suffix_len > 0)
{
$id .= substr(sprintf('%010u', mt_rand()), 0, $suffix_len);
}
return $id;
}
复制代码 代码如下:
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
PHP_VERSION for($i = 0; $i $hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
function random2($length, $numeric = 0) {
PHP_VERSION $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
$seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
$hash = '';
$max = strlen($seed) - 1;
for($i = 0; $i $hash .= $seed[mt_rand(0, $max)];
}
return $hash;
}
?>
复制代码 代码如下:
const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
/**
* 将64进制的数字字符串转为10进制的数字字符串
* @param $m string 64进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex64to10($m, $len = 0) {
$m = (string)$m;
$hex2 = '';
$Code = KeyCode;
for($i = 0, $l = strlen($Code); $i $KeyCode[] = $Code[$i];
}
$KeyCode = array_flip($KeyCode);
for($i = 0, $l = strlen($m); $i $one = $m[$i];
$hex2 .= str_pad(decbin($KeyCode[$one]), 6, '0', STR_PAD_LEFT);
}
$return = bindec($hex2);
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将10进制的数字字符串转为64进制的数字字符串
* @param $m string 10进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex10to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = decbin($m);
$hex2 = str_rsplit($hex2, 6);
$hex64 = array();
foreach($hex2 as $one) {
$t = bindec($one);
$hex64[] = $KeyCode[$t];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 将16进制的数字字符串转为64进制的数字字符串
* @param $m string 16进制的数字字符串
* @param $len integer 返回字符串长度,如果长度不够用0填充,0为不填充
* @return string
* @author 野马
*/
function hex16to64($m, $len = 0) {
$KeyCode = KeyCode;
$hex2 = array();
for($i = 0, $j = strlen($m); $i $hex2[] = str_pad(base_convert($m[$i], 16, 2), 4, '0', STR_PAD_LEFT);
}
$hex2 = implode('', $hex2);
$hex2 = str_rsplit($hex2, 6);
foreach($hex2 as $one) {
$hex64[] = $KeyCode[bindec($one)];
}
$return = preg_replace('/^0*/', '', implode('', $hex64));
if($len) {
$clen = strlen($return);
if($clen >= $len) {
return $return;
}
else {
return str_pad($return, $len, '0', STR_PAD_LEFT);
}
}
return $return;
}
/**
* 功能和PHP原生函数str_split接近,只是从尾部开始计数切割
* @param $str string 需要切割的字符串
* @param $len integer 每段字符串的长度
* @return array
* @author 野马
*/
function str_rsplit($str, $len = 1) {
if($str == null || $str == false || $str == '') return false;
$strlen = strlen($str);
if($strlen $headlen = $strlen % $len;
if($headlen == 0) {
return str_split($str, $len);
}
$return = array(substr($str, 0, $headlen));
return array_merge($return, str_split(substr($str, $headlen), $len));
}
$a=idate("U");
echo "\r\n
e:" . hex10to64($a);
echo "\r\n
e:" . hex64to10(hex10to64($a));
复制代码 代码如下:
function dec2s4($dec) {
$base = '0123456789_$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$result = '';
do {
$result = $base[$dec % 64] . $result;
$dec = intval($dec / 64);
} while ($dec != 0);
return $result;
}
function s42dec($sixty_four) {
$base_map = array ( '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '_' => 10, '$' => 11, 'a' => 12, 'b' => 13, 'c' => 14, 'd' => 15, 'e' => 16, 'f' => 17, 'g' => 18, 'h' => 19, 'i' => 20, 'j' => 21, 'k' => 22, 'l' => 23, 'm' => 24, 'n' => 25, 'o' => 26, 'p' => 27, 'q' => 28, 'r' => 29, 's' => 30, 't' => 31, 'u' => 32, 'v' => 33, 'w' => 34, 'x' => 35, 'y' => 36, 'z' => 37, 'A' => 38, 'B' => 39, 'C' => 40, 'D' => 41, 'E' => 42, 'F' => 43, 'G' => 44, 'H' => 45, 'I' => 46, 'J' => 47, 'K' => 48, 'L' => 49, 'M' => 50, 'N' => 51, 'O' => 52, 'P' => 53, 'Q' => 54, 'R' => 55, 'S' => 56, 'T' => 57, 'U' => 58, 'V' => 59, 'W' => 60, 'X' => 61, 'Y' => 62, 'Z' => 63, );
$result = 0;
$len = strlen($sixty_four);
for ($n = 0; $n $result *= 64;
$result += $base_map[$sixty_four{$n}];
}
return $result;
}
$a=idate("U");
var_dump(dec2s4($a));
var_dump(s42dec(dec2s4($a)));
复制代码 代码如下:
$strarr = array();
$time1 = microtime(true);
for($i = 0; $i $str = idate("U")+$i;
$strarr[] = "{$i}->$str\r\n
";
}
$time2 = microtime(true);
$time3 = $time2 - $time1;
$time1 = microtime(true);
for($i = 0; $i $str = dec2s4(idate("U")+$i);
$strarr[] = "{$i}->$str\r\n
";
}
$time2 = microtime(true);
echo "\r\n
运行10000次用时(秒):" . ($time2 - $time1 - $time3);