If you are building a website that will display photos to users of the site, is it important to always give each photo a unique name?
For example, instead of naming a photo file using an index number such as "00001" like this:


would it be better to name it using a unique random character string like this?


My thinking is that if the photo's owner were to want to replace that photo with a different photo and you give the new photo the same name as the old photo, then when another user views the photo, their browser will see that same photo name in its cache and display the old photo rather than a newer one.

I should add that the photos will be stored on a CDN such as Akamai or Amazon Cloudfront.

  • 1
    This sounds like a programming concern. I would make it as simple as you can for the user and as flexible as you can for your needs. However, from a search perspective, it is always better that the file name describe the photo. Otherwise, the file name does not matter, as long as you and the user can manage it appropriately. I guess it would depend upon your ability to code.
    – closetnoc
    Oct 12, 2015 at 16:51
  • This is true but my real question is about caching. If you give a photo a particular name and then give a different photo that same name, will the browser display the old photo from its cache since both the new and old photos have the same name. If so, then it would seem like a good idea to always give photos unique names to avoid this cache confusion. Sorry if this seems like a stupid question but I'm still learning.
    – Jim
    Oct 12, 2015 at 16:55
  • Gotcha!! Ok. I am not as sure about caching as much as I like. I used to know this stuff cold when I was working (retired). I think it would depend on where the caching takes place. If on the web server, I would assume that the modification date would change and the new image would appear. In web browser or using a 3rd party cache, that may not happen. Some proxy caches do make head requests periodically to check the original resource. Which ones? I have no idea. I seem to remember that I had to manage cache times in hardware sometimes.
    – closetnoc
    Oct 12, 2015 at 17:04
  • Thanks. I should add that ultimately I plan to store users' photos on a CDN such as Akamai or Amazon Cloudfront so I'm wondering if the CDN will notice when a photo file has changed and always serve user's the latest version.
    – Jim
    Oct 12, 2015 at 17:06
  • 1
    I suggest the title and intro paragraph be edited to remove the confusion about your focus on caching as it was only the comments that made it clear. Oct 12, 2015 at 21:10

2 Answers 2


Why not use timestamps at the end of photo names for fresh photos?

For example:


And the difference between the two files is one second because of the values of the numbers. If you want instead, you can use dates, but timestamps are more accurate to the second if users constantly update photos.

Just make sure the HTML page is updated as well to reflect the new photo file name and the cache for the HTML should be smaller than the amount of patience the most impatient guest will likely have for your site. This means use no caching for the HTML and tons of caching for the image filenames.


The question is different, but I believe that the accepted answer to this question might also work for you. tl;dr, you can generate a string of characters for each file and tack it onto the src in a given element. Because the string will be unique, the browser will interpret the file as something new even if it has the same name. Anybody looking at the file directly or saving it will just see it as its name without some random string or super-long index number. You should be able to do this when your page is generated (no need for extra database entries), as long as the string creation is algorithmic based on some data about the image. As long as the image stays the same, it'll be the same string.

  • Thanks for this suggestion. The other question you referred to was helpful in that it describes my problem perfectly. However, I don't think I'll use the query string approach because 1) I don't have a deep enough understanding of HTTP and Nginx to implement it in the time I have available; and 2) it would seem like you still need to keep track of the query string value someplace. If that's the case, I might as well just store a unique photo ID in a database table.
    – Jim
    Oct 13, 2015 at 18:06
  • You don't require messing with the server at all. You can use HTML5 to get the value of an arbitrary pixel in the image (imgData.data[60], imgData.data[61], and imgData.data[62] would give you the RGB value of the 15th pixel in the image) and then pass that to the page output. Or you could do the same with a database. The benefit of the query string method is that anyone who interacts with the file (your user or a viewer) will see the file name that the user intended, instead of the human-illegible mess you see in Facebook links. Oct 14, 2015 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.