I was wondering how can I make an auto-generated id like the one on Facebook in every pictures like this:
I need to put an id to the file I upload because it worries me when the user uploaded the same file name on the same date it will confuse the API. I am sorry that I can’t show you any codes, because I really don’t know where to start.
Here you can see I uploaded 06 twice
Also, I don’t want to use an auto increment ID on this because I am planning to save the ID as the file name of the file inside my server.
2
Answers
You can use date to produce the date and time . Whatever the data comes convert it into miliseconds as per programming language you use. Concate that miliseconds with your file name. Like miniseconds + your file name. So every file name will be unique. And you can maintain two rows : 1. Original media name 2. Modified Media name
Store unique name in modified media name and original name in original media name.
When ever user request for the photo send the modified media name as the path and original media name to display as the photo name.
For this, I would recommend using a hash such as one of the SHA hash functions or MD5. You could generate this hash through JavaScript with a library such as crypto-js, but it would probably be easier to just generate the hash on your web server.
There is a possibility of hash collisions, but this is extremely unlikely even for a relatively weak hash like MD5. If you use a hash like SHA-256, there probably haven’t been any identical hashes ever generated. However, by using a hash function, you get the benefit of avoiding duplicate images being stored.