2

Suppose many users uploading their pics at one time. So how could i generate a unique id for every pic which could never match with other generated id.

$uniqId = time().'_'.rand();

or should is use.

uniqid();
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Naresh
  • 2,761
  • 10
  • 45
  • 78
  • i am on local host so cant check unique id's... Is this best for every hit in nano sec also ?? – Naresh Aug 14 '13 at 06:58
  • You can use the server's UNIX timestamp also, that's never the same. and/or `mt_rand()` - I sometimes use a mix of both. – Funk Forty Niner Aug 14 '13 at 06:58
  • 1
    Consider using a [`uuid`](http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid/15875555#15875555). – Ja͢ck Aug 14 '13 at 06:59
  • 1
    If you are storing information about the pictures in the database, I would use the auto-increment id. – jeroen Aug 14 '13 at 07:02
  • @PuzzledBoy Then what about a mix of `sha1 - uniqid - mt_rand` or something like `$file_id = md5(rand(1, 7) . rand(1, 4) . rand(1, 6));` – Funk Forty Niner Aug 14 '13 at 07:09
  • @Fred There are well defined standards for generating unique identifiers, like the one under Linked (right side). – Ja͢ck Aug 14 '13 at 07:13
  • @Jack It's up to the OP to decide then, which one he feels will be best suited. – Funk Forty Niner Aug 14 '13 at 07:14
  • @Fred i have read some where that some time md5() and rand() generates a same id.. is your method perfect .. take example of FaceBook which method they used ?? – Naresh Aug 14 '13 at 07:18
  • oww..... i am still very confused... :( – Naresh Aug 14 '13 at 07:21
  • @PuzzledBoy The chances of a collision/duplicate (depending on which method and how many times they are used) can literally be 1 in 50 million if not more. I don't have those exact stats, but I doubt you will be hitting those numbers any time in this lifetime (wink) Find out what Facebook uses. **When in doubt, use Jack's method.** – Funk Forty Niner Aug 14 '13 at 07:22

3 Answers3

9

Using the uniqid() PHP function will probably be your best method, since no DB is used for files sent.

As per the PHP manual: uniqid — Generate a unique ID.

"Can be useful, for instance, if you generate identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond."

Quoted from the PHP manual.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
7

The function uniqid() generate a different id each nanosecond so it'll be unique most of the time.

If you expect tons of users uploading at the same time in a shared environment (multiple servers), then it become quite possible for multiple requests to be handled at the same nanosecond across all servers.

The function accepts two arguments to help increase the uniqueness of the result:

  1. The prefix (first argument) could be set to the server name to avoid cross server name collisions.
  2. The "more entropy" (second argument) increases the likelihood that the result will be unique.

For example:

echo uniqid($my_hostname, true);

Besides the server name you can add other sources of entropy such as the user id (if applicable).

If you are using a database to keep track of images, the other way is to use the (auto_increment) primary key which is unique for each row (so, the database takes care of the uniqueness for you)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Atrakeur
  • 4,126
  • 3
  • 17
  • 22
3

You can generate uniqueid by mysql table.

create table ids(id int auto_increment primary key);

When users uploading their pics, write sql like this

insert into ids () values();
select last_insert_id();
Larry.Z
  • 3,694
  • 1
  • 20
  • 17
  • thankx for your ans .but just read my question again here is no any MySql or inserting query related problem describe... i just only want to create a name of pic in nano seconds.. thannx – Naresh Aug 14 '13 at 07:05