I’ve repeatedly come up with the issue of “where do I put this file” and even “what should I name this file”. There’s a bunch of things to consider.

  • Is it important that the original name of the file persists if someone were to “Save As” on it. That is, do we need to reference the original filename in the url it’s accessed from
  • Is it important that the image cant be guessed. Sometimes we want to upload images that aren’t secret, but are kind of semi-secret, for a bit anyway. For example, maybe an image on a news item that’s there on the website ready to go, but is under embargo.
  • Is there a file called that already - we don’t want to overwrite it.

It gets more complicated when you think of other domain specific issues around the whole path. In the e-commerce scenario you might want to rename the image to the most SEO friendly thing you can manage. But perhaps that should be spread around the URL for a bit.

Let’s get into some examples. Let’s do e-commerce - we’re selling Heinz Baked beans. We have three images. How about:

  • /images/uploads/products/large/Heinz-Baked-Beans.jpg

Then if we want a smaller version we can put replace the large for small, and the uploads is clearly going to be backed up as part of the data of the site, and not something that’s going to go into a repository, like git.

But it might make sense to do something like this:

  • /images/uploads/products/heinz/large/baked-beans-idfh3asd.jpg

We know on the product that the brand is Heinz, and by putting all the images of Heinz in the same folder we can gauge how big the folders of the various brands are getting and spot outliers. That is, if we care about such things as not having masses of data. Which so many people don’t care about. We also have a random code at the end, which could be based on an md5sum of the file’s contents, in order to ensure that should another version get uploaded, they all have a different file name.

Why might that be important? Because you want your images cached, by whatever will cache it. And my experience is that the best thing, really, is to cache for ages and make sure you change the static files’ names as the content changes.

I think this makes for a pretty good image url. You could go further and put in some product IDs in there, which will help if you find your images folder is getting too large. You would be able to look at the name (or path) of your image and relate it back to a specific product in your database. If that product no longer exists, or no longer needs an image you can delete it. That way you’ve got a way back to clean your directories of wasted space with unused images.

Of course the same applies to videos, and other types of file that might want to be shared.