panthro's avatar

Naming conventions for images with the same name - but not the same content?

My users can upload images to my site, images revolve around a product, I name the image file names after the product name.

What should I do when more than one image exists for the same product, for example, a user uploads a picture of their Nintendo Switch, the system creates a file called "nintendo-switch.jpg", now another user uploads a different picture of a Nintendo Switch, what should the system name the file?

I've considered numerical numbering: "nintendo-switch.jpg", "nintendo-switch-1.jpg", "nintendo-switch-2.jpg".

I've considered adding a hash to each image: "nintendo-switch-aso92jd.jpg", "nintendo-switch-87676as.jpg".

I've considered storing each image in it's own folder, corresponding to an id in a database and calling all the images the same name: "/22/nintendo-switch.jpg", "23/nintendo-switch.jpg".

Please note, there is no way for the user to name the images themselves, I just need a naming convention that I can code that is seo freindly.

0 likes
7 replies
mrbegginerak's avatar

yes , they all are good way, but remember one thing , getting users uploaded file extension and clientoriginalname methods are unsafe , to store the files .

1 like
Snapey's avatar

I don't think image names has much to do with SEO, but whatever.

Personally I would use a random number for the whole filename, but you could append a short random string (NOT a hash) or a timestamp.

Putting the images in their own folder is a good idea if the image numbers will reach into 1000's as it starts to become an issue to manage the folder when it has 1000's of nodes.

1 like
panthro's avatar

@Snapey thanks for your advice, I didnt think it was such an issue on services like S3?

Anyway, what would you suggest for folder names? An id of the row from the media table that manages the images? Or a uuid so it obscures my backend database structure?

Snapey's avatar

@panthro since Amazon charges for listing folder contents, having large numbers of images in one folder could prove expensive.

If each item has multiple images then put them in a folder. Perhaps look at HashIDs for obfuscation of your ID structure.

But if each item is expected to only have one image then putting it in a folder would be pointless.

1 like
PovilasKorop's avatar

@panthro I would just use Spatie Medialibrary package that takes care of everything, putting every media in its own folder.

But I do agree with @snapey - I don't think image names give much SEO benefits these days. Unless someone searches on Google Images and then lands on your website from there, but it's probably a small percentage of people.

1 like
panthro's avatar

@PovilasKorop thanks, Spatie puts a single image in a single folder, but @snapey says this approach is pointless. Also, I have a few other requirements unrelated to the question which means the Spatie package would not be suitable.

I would be in a situation where one row, would relate to one image.

Kinda stuck with the best practice of the file/folder structure.

All uploads would sit in an uploads folder on S3. But should I have a folder in here for each db row?

/uploads/1/some-image.jpg /uploads/2/some-image.jpg

Or shall I just put everything in uploads:

/uploads/some-image.jpg /uploads/some-image.jpg

Or something else? Thoughts very welcome!

PovilasKorop's avatar
Level 11

@panthro in my opinion, you're overthinking it. I don't see the scenario where this decision actually make a big difference. It's your personal preference.

1 like

Please or to participate in this conversation.