欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

基于php上傳圖片重命名的6種解決方法的詳細(xì)介紹_PHP教程

編輯Tag賺U幣

推薦:基于curl數(shù)據(jù)采集之單頁面采集函數(shù)get_html的使用
在做數(shù)據(jù)采集時(shí)經(jīng)常要使用到curl+正則的方式采集需要的數(shù)據(jù) 根據(jù)自己的工作經(jīng)驗(yàn) 把自己寫的一些常用自定義函數(shù) 與大家來分享 如果有寫得不恰當(dāng)?shù)牡胤?請(qǐng)多多指教

一,適用場景:無法使用從數(shù)據(jù)庫中返回的自增長數(shù)字,給上傳圖片重命名。

這是圖片或文件上傳的流程決定的。
一般圖片上傳處理過程是,先上傳圖片到服務(wù)器,重命名之后,插入到數(shù)據(jù)庫。
也就是說,在數(shù)據(jù)庫中非常容易獲得的自增長id,無法用于給上傳的圖片重命名,來避免文件名稱的重復(fù),
而采用從數(shù)據(jù)庫中獲取最大id加1的方式,增加了數(shù)據(jù)庫連接的次數(shù),不適用于高并發(fā)和數(shù)據(jù)量巨大的情況;

二,常規(guī)方案:

1,guid:32 字符十六進(jìn)制數(shù)。
格式:GUID 的格式為“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中每個(gè) x 是 0-9 或 a-f 范圍內(nèi)的一個(gè)32位十六進(jìn)制數(shù)。例如:6F9619FF-8B86-D011-B42D-00C04FC964FF 即為有效的 GUID 值。

優(yōu)點(diǎn):幾乎不會(huì)重復(fù);
缺點(diǎn):對(duì)于給上傳的圖片重命名,還是過長了。
用法:

復(fù)制代碼 代碼如下:m.dounai2.com

/*
com_create_guid()是php5版本支持的功能,對(duì)于不支持的版本,可以自己進(jìn)行定義;
*/
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 一樣會(huì)輸出32 字符十六進(jìn)制數(shù),區(qū)別是guid是隨機(jī)產(chǎn)生的,md5需要根據(jù)輸入的數(shù)據(jù)生成。
例子,
復(fù)制代碼 代碼如下:m.dounai2.com

<?php
$str = "Hello";
echo md5($str);
?>

輸出,
復(fù)制代碼 代碼如下:m.dounai2.com

8b1a9953c4611296a827abf8c47804d7

優(yōu)點(diǎn):可以根據(jù)輸入的種子數(shù)據(jù)來控制輸出的數(shù)值,如果種子數(shù)據(jù)是規(guī)律性不重復(fù)的,通過md5可以對(duì)數(shù)據(jù)進(jìn)行保護(hù),產(chǎn)生很大的混淆作用。
缺點(diǎn):32位字符過長;需提供不重復(fù)的種子數(shù)據(jù);
用法:高并發(fā),以秒為種子數(shù)據(jù),仍然會(huì)出現(xiàn)重復(fù)現(xiàn)象。
復(fù)制代碼 代碼如下:m.dounai2.com

<?php
/*
*結(jié)合time()函數(shù)使用,以1970年到當(dāng)前時(shí)間的秒數(shù)作為種子數(shù)。
*/
$str=time();
echo md5($str);
?>

3,uniqid():返回13或23位字符串。
對(duì)于我們目的來說,uniqid()像是md5()的改進(jìn)版,尤其是我們可以采用差異性標(biāo)識(shí)作為字符串前綴,可以降低重復(fù)命名出現(xiàn)的幾率。
對(duì)于非高并發(fā)等極端情況,推薦使用此函數(shù),已經(jīng)可以滿足一般性需求。
詳細(xì)說明,
定義:uniqid() 函數(shù)基于以微秒計(jì)的當(dāng)前時(shí)間,生成一個(gè)唯一的 ID。
用法:uniqid(prefix,more_entropy)
說明:prefix可以為輸出的字符串添加前綴,示例如下,more_entropy參數(shù)為true時(shí),將輸出23位字符串。
復(fù)制代碼 代碼如下:m.dounai2.com

<?php
var_dump(uniqid());
var_dump(uniqid("a"));
?>

輸出結(jié)果為:
復(fù)制代碼 代碼如下:m.dounai2.com

string(13) "51734aa562254" string(14) "a51734aa562257"

優(yōu)點(diǎn):13位字符串長度,是可以接受的文件命名長度;可以添加前綴,結(jié)果包含數(shù)據(jù)混淆,能夠避免反推原始數(shù)據(jù)。
缺點(diǎn):同md5相似,高并發(fā),以秒為種子數(shù)據(jù),仍然會(huì)出現(xiàn)重復(fù)現(xiàn)象。

三、升級(jí)版方案:

1,fast_uuid:返回17位數(shù)字。
有點(diǎn)像uniqid()的不完全定制版,這個(gè)函數(shù)里面出現(xiàn)的“種子數(shù)開始時(shí)間”概念很有啟發(fā)性。
time()和uniqid()中默認(rèn)用到的時(shí)間都是從1970年開始計(jì)算的,長度有十位(1366512439),采用“種子數(shù)開始時(shí)間”能夠縮小這個(gè)數(shù)值,因?yàn)槲覀儗?shí)際上需要的,僅僅是一個(gè)能夠自動(dòng)增長的數(shù)值即可。
起始時(shí)間自定義以后,除了減少長度,還能夠起到混淆的作用。

復(fù)制代碼 代碼如下:m.dounai2.com

/*
* 參數(shù) suffix_len指定 生成的 ID 值附加多少位隨機(jī)數(shù),默認(rèn)值為 3。
* 感謝“Ivan Tan|譚俊青 DrinChing (at) Gmail.com”提供的算法。
* @param int suffix_len
* @return string
*/
function fast_uuid($suffix_len=3){
//! 計(jì)算種子數(shù)的開始時(shí)間
$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;
}

輸出,
復(fù)制代碼 代碼如下:m.dounai2.com

29832412631099013

2,time()+隨機(jī)數(shù):

上例中已經(jīng)出現(xiàn)了隨機(jī)數(shù)的使用,是為了解決一秒下發(fā)生的多次請(qǐng)求。提供兩個(gè)函數(shù)如下,

復(fù)制代碼 代碼如下:m.dounai2.com

<?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+秒+隨機(jī)數(shù)。其中“userid+秒”10進(jìn)制轉(zhuǎn)64進(jìn)制,縮減位數(shù);

說明:
1,userid: 64進(jìn)制最大值“ZZZZ"轉(zhuǎn)換為十進(jìn)制等于”16777215“,”ZZZ“轉(zhuǎn)換為十進(jìn)制最大值等于”262143“;
2,秒:設(shè)置自己的時(shí)間起點(diǎn)。
$less=time()-strtotime('2012-4-21'); 轉(zhuǎn)換為64進(jìn)制”1SpRe“,5位
$less=time()-strtotime('2013-3-21'); 轉(zhuǎn)換為64進(jìn)制”_jHY“;4位
3,隨機(jī)數(shù):使用random(3)生成3位隨機(jī)數(shù);

最終結(jié)果:
4位userid+4位秒+3位隨機(jī)數(shù)=11位字符串。雖然與uniqid()結(jié)果看上去相似,但是強(qiáng)壯度有所提高。

五,十進(jìn)制轉(zhuǎn)64進(jìn)制算法:

1,算法1:

復(fù)制代碼 代碼如下:m.dounai2.com

View Code

const KeyCode = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';

/**
* 將64進(jìn)制的數(shù)字字符串轉(zhuǎn)為10進(jìn)制的數(shù)字字符串
* @param $m string 64進(jìn)制的數(shù)字字符串
* @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進(jìn)制的數(shù)字字符串轉(zhuǎn)為64進(jìn)制的數(shù)字字符串
* @param $m string 10進(jìn)制的數(shù)字字符串
* @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進(jìn)制的數(shù)字字符串轉(zhuǎn)為64進(jìn)制的數(shù)字字符串
* @param $m string 16進(jìn)制的數(shù)字字符串
* @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原生函數(shù)str_split接近,只是從尾部開始計(jì)數(shù)切割
* @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:
復(fù)制代碼 代碼如下:m.dounai2.com

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,算法效率測(cè)試:
復(fù)制代碼 代碼如下:m.dounai2.com

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 />運(yùn)行10000次用時(shí)(秒):" . ($time2 - $time1 - $time3);


4,測(cè)試結(jié)果
算法1:0.1687250137329
算法2:0.044965028762817
5,結(jié)論:算法1雖然效率上差一些,但是可以把md5生成的16進(jìn)制轉(zhuǎn)化為64進(jìn)制,能夠使用在必須使用md5的環(huán)境下縮短字符串。

六,總結(jié)
本文涉及了上傳圖片重命名可以能使用的幾種方法,其中關(guān)鍵點(diǎn)是使用10進(jìn)制轉(zhuǎn)換為64進(jìn)制來進(jìn)行字符串的縮減。
例如,使用fast_uuid生成的17位數(shù)字,轉(zhuǎn)換為64進(jìn)制僅有7位字符;
具體使用,可以根據(jù)自身情況靈活使用,希望對(duì)大家有所幫助。

參考文獻(xiàn):

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()函數(shù)說明:http://www.w3school.com.cn/php/func_string_md5.asp
4,time()函數(shù)說明:http://www.w3school.com.cn/php/func_date_time.asp
5,uniqid()函數(shù)說明:http://www.w3school.com.cn/php/func_misc_uniqid.asp

分享:基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用
本篇文章介紹了,基于curl數(shù)據(jù)采集之正則處理函數(shù)get_matches的使用。需要的朋友參考下

來源:模板無憂//所屬分類:PHP教程/更新時(shí)間:2013-04-29
相關(guān)PHP教程