I’m uploading files to a site for our company using CGI. In the admin panel, employees can upload images to correspond to a specific object that is later stored in JSON. All of this works, but if Employee A uploads dog.png and then Employee B uploads dog.png. Employee B’s picture will override Employee A’s picture and both will reference the same picture. I thought about getting a hash of the image and naming it it’s own hash, problem is, reading the file prior to upload is a bit sketchy with browser support (security issues) and saving it to the server, getting the hash, re-saving, and then deleting the old file is slow and hacked up.
My Question
With this in mind, what is the best way to give each file a unique name? The name of the file doesn’t matter at all, as long as it follows saving conventions (i.e. no / or .). I also have to be able to generate the name before the file is uploaded.
What I Am Currently Using
- JQuery
- Javascript
- CGI (Python)
I have a similar system for uploading images to my website for products.
Each product has its own folder, which is the ID of the product in the database, but this is not really relevant.
The images are given names as they’re uploaded simply in the format
This works fine as as no time stamp can be repeated. It fails if two items are uploaded at exactly the same time, to the second, so I guess the separate folders help here.
You could do away with the folders if the frequency of upload is quite low, or simply append an additional random number, or maybe a user name or something?