PHP图片上传与重命名六种方法总结

WBOY
Freigeben: 2016-07-25 08:54:26
Original
1593 Leute haben es durchsucht
  1. /*
  2. com_create_guid()是php5版本支持的功能,对于不支持的版本,可以自己进行定义
  3. */
  4. function guid(){
  5. if (function_exists('com_create_guid')){
  6. return com_create_guid();
  7. }else{
  8. mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  9. echo(mt_rand());
  10. $charid = strtoupper(md5(uniqid(rand(), true)));
  11. $hyphen = chr(45);// "-"
  12. $uuid = chr(123)// "{"
  13. .substr($charid, 0, 8).$hyphen
  14. .substr($charid, 8, 4).$hyphen
  15. .substr($charid,12, 4).$hyphen
  16. .substr($charid,16, 4).$hyphen
  17. .substr($charid,20,12)
  18. .chr(125);// "}"
  19. return $uuid;
  20. }
  21. }
复制代码

2、MD5 与guid一样会输出32字符十六进制数,区别是guid是随机产生的,md5需要根据输入的数据生成。 例子:

  1. $str = "Hello";
  2. echo md5($str);
  3. ?>
复制代码

输出: 8b1a9953c4611296a827abf8c47804d7 优点:可以根据输入的种子数据来控制输出的数值,如果种子数据是规律性不重复的,通过md5可以对数据进行保护,产生很大的混淆作用; 缺点:32位字符过长;需提供不重复的种子数据; 用法:高并发,以秒为种子数据,仍然会出现重复现象。

  1. /*
  2. *结合time()函数使用,以1970年到当前时间的秒数作为种子数
  3. */
  4. $str=time();
  5. echo md5($str);
  6. ?>
复制代码

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

  1. var_dump(uniqid());
  2. var_dump(uniqid("a"));
  3. ?>
复制代码

输出结果为: string(13) "51734aa562254" string(14) "a51734aa562257" 优点:13位字符串长度,是可以接受的文件命名长度;可以添加前缀,结果包含数据混淆,能够避免反推原始数据; 缺点:同md5相似,高并发,以秒为种子数据,仍然会出现重复现象。 三、升级版方案 1、fast_uuid:返回17位数字 有点像uniqid()的不完全定制版,这个函数里面出现的“种子数开始时间”概念很有启发性。 time()和uniqid()中默认用到的时间都是从1970年开始计算的,长度有十位(1366512439),采用“种子数开始时间”能够缩小这个数值,因为我们实际上需要的,仅仅是一个能够自动增长的数值即可。 起始时间自定义以后,除了减少长度,还能够起到混淆的作用。

  1. /*
  2. * 参数 suffix_len指定 生成的 ID 值附加多少位随机数,默认值为 3
  3. * @param int suffix_len
  4. * @return string
  5. */
  6. function fast_uuid($suffix_len=3){
  7. //! 计算种子数的开始时间
  8. $being_timestamp = strtotime('2013-3-21');
  9. $time = explode(' ', microtime());
  10. $id = ($time[1] - $being_timestamp) . sprintf('%06u', substr($time[0], 2, 6));
  11. if ($suffix_len > 0)
  12. {
  13. $id .= substr(sprintf('%010u', mt_rand()), 0, $suffix_len);
  14. }
  15. return $id;
  16. }
复制代码

输出: 29832412631099013 2、time()+随机数 上例中已经出现了随机数的使用,是为了解决一秒下发生的多次请求。 提供两个函数:

  1. function random($length) {
  2. $hash = '';
  3. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  4. $max = strlen($chars) - 1;
  5. PHP_VERSION for($i = 0; $i $hash .= $chars[mt_rand(0, $max)];
  6. }
  7. return $hash;
  8. }
  9. function random2($length, $numeric = 0) {
  10. PHP_VERSION $seed = base_convert(md5(print_r($_SERVER, 1).microtime()), 16, $numeric ? 10 : 35);
  11. $seed = $numeric ? (str_replace('0', '', $seed).'012340567890') : ($seed.'zZ'.strtoupper($seed));
  12. $hash = '';
  13. $max = strlen($seed) - 1;
  14. for($i = 0; $i $hash .= $seed[mt_rand(0, $max)];
  15. }
  16. return $hash;
  17. }
  18. ?>
复制代码

四、最终方案 思路: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()结果看上去相似,但是强壮度有所提高。 1 2 下一页 尾页



Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!