基于php上传图片重命名的6种解决方法的详细介绍


Posted in PHP onApril 28, 2013

一,适用场景:无法使用从数据库中返回的自增长数字,给上传图片重命名。

这是图片或文件上传的流程决定的。
一般图片上传处理过程是,先上传图片到服务器,重命名之后,插入到数据库。
也就是说,在数据库中非常容易获得的自增长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;
   }
}

2,MD5:
与guid 一样会输出32 字符十六进制数,区别是guid是随机产生的,md5需要根据输入的数据生成。
例子,
<?php
$str = "Hello";
echo md5($str);
?>

输出,
8b1a9953c4611296a827abf8c47804d7

优点:可以根据输入的种子数据来控制输出的数值,如果种子数据是规律性不重复的,通过md5可以对数据进行保护,产生很大的混淆作用。
缺点:32位字符过长;需提供不重复的种子数据;
用法:高并发,以秒为种子数据,仍然会出现重复现象。
<?php
/*
*结合time()函数使用,以1970年到当前时间的秒数作为种子数。
*/
$str=time();
echo md5($str);
?>

3,uniqid():返回13或23位字符串。
对于我们目的来说,uniqid()像是md5()的改进版,尤其是我们可以采用差异性标识作为字符串前缀,可以降低重复命名出现的几率。
对于非高并发等极端情况,推荐使用此函数,已经可以满足一般性需求。
详细说明,
定义:uniqid() 函数基于以微秒计的当前时间,生成一个唯一的 ID。
用法:uniqid(prefix,more_entropy)
说明:prefix可以为输出的字符串添加前缀,示例如下,more_entropy参数为true时,将输出23位字符串。
<?php
var_dump(uniqid());
var_dump(uniqid("a"));
?>

输出结果为:
string(13) "51734aa562254" string(14) "a51734aa562257"

优点:13位字符串长度,是可以接受的文件命名长度;可以添加前缀,结果包含数据混淆,能够避免反推原始数据。
缺点:同md5相似,高并发,以秒为种子数据,仍然会出现重复现象。

三、升级版方案:

1,fast_uuid:返回17位数字。
有点像uniqid()的不完全定制版,这个函数里面出现的“种子数开始时间”概念很有启发性。
time()和uniqid()中默认用到的时间都是从1970年开始计算的,长度有十位(1366512439),采用“种子数开始时间”能够缩小这个数值,因为我们实际上需要的,仅仅是一个能够自动增长的数值即可。
起始时间自定义以后,除了减少长度,还能够起到混淆的作用。

/*
* 参数 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;
    }

输出,
29832412631099013

2,time()+随机数:

上例中已经出现了随机数的使用,是为了解决一秒下发生的多次请求。提供两个函数如下,

<?php
function random($length) {
    $hash = '';
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($chars) - 1;
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    for($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}
function random2($length, $numeric = 0) {
    PHP_VERSION < '4.2.0' ? mt_srand((double)microtime() * 1000000) : mt_srand();
    $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 < $length; $i++) {
        $hash .= $seed[mt_rand(0, $max)];
    }
    return $hash;
}
?>

四,最终方案:

思路:userid+秒+随机数。其中“userid+秒”10进制转64进制,缩减位数;

说明:
1,userid: 64进制最大值“ZZZZ"转换为十进制等于”16777215“,”ZZZ“转换为十进制最大值等于”262143“;
2,秒:设置自己的时间起点。
$less=time()-strtotime('2012-4-21'); 转换为64进制”1SpRe“,5位
$less=time()-strtotime('2013-3-21'); 转换为64进制”_jHY“;4位
3,随机数:使用random(3)生成3位随机数;

最终结果:
4位userid+4位秒+3位随机数=11位字符串。虽然与uniqid()结果看上去相似,但是强壮度有所提高。

五,十进制转64进制算法:

1,算法1:

View Code 
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 < $l; $i++) {
            $KeyCode[] = $Code[$i];
        }
        $KeyCode = array_flip($KeyCode);
        for($i = 0, $l = strlen($m); $i < $l; $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 < $j; ++$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 <= $len) return array($str);
        $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<br />e:" . hex10to64($a);
echo "\r\n<br />e:" . hex64to10(hex10to64($a));

2,算法2:
View Code 
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 < $len; $n++) {  
        $result *= 64;  
        $result += $base_map[$sixty_four{$n}];  
    }  
    return $result;  
}  
$a=idate("U");
var_dump(dec2s4($a));  
var_dump(s42dec(dec2s4($a)));

3,算法效率测试:
View Code 
$strarr = array();
$time1 = microtime(true);
for($i = 0; $i < 10000; ++$i) {
     $str = idate("U")+$i;
     $strarr[] = "{$i}->$str\r\n<br>";
 }
 $time2 = microtime(true);
 $time3 = $time2 - $time1;
 $time1 = microtime(true);
 for($i = 0; $i < 10000; ++$i) {
     $str = dec2s4(idate("U")+$i);
    $strarr[] = "{$i}->$str\r\n<br>";
}
$time2 = microtime(true);
echo "\r\n<br />运行10000次用时(秒):" . ($time2 - $time1 - $time3);

4,测试结果
算法1:0.1687250137329
算法2:0.044965028762817
5,结论:算法1虽然效率上差一些,但是可以把md5生成的16进制转化为64进制,能够使用在必须使用md5的环境下缩短字符串。

六,总结
本文涉及了上传图片重命名可以能使用的几种方法,其中关键点是使用10进制转换为64进制来进行字符串的缩减。
例如,使用fast_uuid生成的17位数字,转换为64进制仅有7位字符;
具体使用,可以根据自身情况灵活使用,希望对大家有所帮助。

参考文献:

1,GUID百度百科:http://baike.baidu.com/view/185358.htm
2,com_create_guid() 官方指南:http://www.php.net/manual/zh/function.com-create-guid.php
3,MD5()函数说明:http://www.w3school.com.cn/php/func_string_md5.asp
4,time()函数说明:http://www.w3school.com.cn/php/func_date_time.asp
5,uniqid()函数说明:http://www.w3school.com.cn/php/func_misc_uniqid.asp

PHP 相关文章推荐
PHP和XSS跨站攻击的防范
Apr 17 PHP
PHP 命名空间实例说明
Jan 27 PHP
PHP运行时强制显示出错信息的代码
Apr 20 PHP
PHP持久连接mysql_pconnect()函数使用介绍
Feb 05 PHP
thinkphp控制器调度使用示例
Feb 24 PHP
PHP读取RSS(Feed)简单实例
Jun 12 PHP
yii实现图片上传及缩略图生成的方法
Dec 04 PHP
PHP实用函数分享之去除多余的0
Feb 06 PHP
PHP程序中的文件锁、互斥锁、读写锁使用技巧解析
Mar 21 PHP
PHP实现普通hash分布式算法简单示例
Aug 06 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
Mar 18 PHP
如何在PHP中读写文件
Sep 07 PHP
PHP基础学习之流程控制的实现分析
Apr 28 #PHP
PHP基础之运算符的使用方法
Apr 28 #PHP
PHP数据类型之整数类型、浮点数的介绍
Apr 28 #PHP
PHP数据类型之布尔型的介绍
Apr 28 #PHP
PHP中最容易忘记的一些知识点总结
Apr 28 #PHP
php中is_null,empty,isset,unset 的区别详细介绍
Apr 28 #PHP
基于php伪静态的实现详细介绍
Apr 28 #PHP
You might like
fleaphp常用方法分页之Pager使用方法
2011/04/23 PHP
使用PHP求两个文件的相对路径
2013/06/20 PHP
smarty中英文多编码字符截取乱码问题解决方法
2014/10/28 PHP
url 编码 js url传参中文乱码解决方案
2010/04/11 Javascript
ExtJS4 Grid改变单元格背景颜色及Column render学习
2013/02/06 Javascript
javascript中不提供sleep功能如何实现这个功能
2014/05/27 Javascript
Javascript中Array.prototype.map()详解
2014/10/22 Javascript
JavaScript获取网页表单action属性的方法
2015/04/02 Javascript
js中this用法实例详解
2015/05/05 Javascript
JS模拟实现Select效果代码
2015/09/24 Javascript
JQuery的attr 与 val区别
2016/06/12 Javascript
jquery计算出left和top,让一个div水平垂直居中的简单实例
2016/07/13 Javascript
AngularJS Select(选择框)使用详解
2017/01/18 Javascript
ajax +NodeJS 实现图片上传实例
2017/06/06 NodeJs
前端路由&amp;webpack基础配置详解
2019/06/10 Javascript
Vue router安装及使用方法解析
2020/12/02 Vue.js
[01:19:35]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#2Fnatic VS OG第二局
2016/03/05 DOTA
python遍历文件夹并删除特定格式文件的示例
2014/03/05 Python
Python中的anydbm模版和shelve模版使用指南
2015/07/09 Python
Python常见字符串操作函数小结【split()、join()、strip()】
2018/02/02 Python
详解Python3的TFTP文件传输
2018/06/26 Python
python 实现A*算法的示例代码
2018/08/13 Python
Python字符串逆序输出的实例讲解
2019/02/16 Python
elasticsearch python 查询的两种方法
2019/08/04 Python
tornado+celery的简单使用详解
2019/12/21 Python
pytorch的梯度计算以及backward方法详解
2020/01/10 Python
pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换
2020/01/13 Python
python的Jenkins接口调用方式
2020/05/12 Python
Keras设置以及获取权重的实现
2020/06/19 Python
Python中Yield的基本用法
2020/10/18 Python
css3打造一款漂亮的卡哇伊按钮
2013/03/20 HTML / CSS
HTML5 video视频字幕的使用和制作方法
2018/05/03 HTML / CSS
mui几种页面跳转方式对比总结概括
2017/08/18 HTML / CSS
酒店管理毕业生自我鉴定
2014/03/02 职场文书
小学生差生评语
2014/12/29 职场文书
Python进程池与进程锁之语法学习
2022/04/11 Python