简体   繁体   中英

Get unique ID for image upload

What I want is upload an image, get unique ID and save this in server. I have two methods to get ID and I want to know if this is the right path for real application.

method 1

round(microtime(true));

method 2

md5(uniqid(rand(), true));

UPDATE: Use cryptographic-ally unique id - uuidV4 to gaureente uniqueness

https://stackoverflow.com/a/31460273/3359432

You can use PHP inbuilt function uniqid . Available since PHP 4

Quoting from PHP official site

<?php
/* A uniqid, like: 4b3403665fea6 */
printf("uniqid(): %s\r\n", uniqid());

/* We can also prefix the uniqid, this the same as 
 * doing:
 *
 * $uniqid = $prefix . uniqid();
 * $uniqid = uniqid($prefix);
 */
printf("uniqid('php_'): %s\r\n", uniqid('php_'));

/* We can also activate the more_entropy parameter, which is 
 * required on some systems, like Cygwin. This makes uniqid()
 * produce a value like: 4b340550242239.64159797
 */
printf("uniqid('', true): %s\r\n", uniqid('', true));
?>

http://php.net/manual/en/function.uniqid.php

You must also read the warning

Warning This function does not guarantee uniqueness of return value. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread. Use more_entropy to increase likelihood of uniqueness.

是的,可以将Md5与当前日期和时间一起使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM