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:
- The prefix (first argument) could be set to the server name to avoid cross server name collisions.
- 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)