taf's avatar
Level 2

Do you randomize uploaded images names or tie them to meaningful IDs?

I'm at the point in a project where I will be uploading images that are associated with a specific controller. I don't foresee that they will ever be used outside this controller's context.

My general process is that I will upload the image and then store the image path/name into the associated table cell. The images will fit into a categorical system where each one will be (Front|Side|Back) + (Category) + (Level).

In naming the files, I am considering two options:

  1. On one hand I like the idea of meaningfully naming the images where an example would look like f-category_id-level_id.extension (i.e. f-1-2.jpg).

  2. On the other I think since I'm storing the name of the file in the database, just randomizing the filename works well too.

In the former, I like that it presents a clean and meaningful folder of files but I worry a little about maintenance. For example, if a category ID or Level ID gets changed, I need to be mindful of keeping file names consistent. In the latter, I have a messy image folder but it's completely independent of any changes to the categories or levels.

I am already leaning more towards one of the approaches but I was hoping to get some feedback before I commit to it. Any thoughts please?

0 likes
2 replies
rawilk's avatar

It depends on if you need SEO. If you don't need it, randomizing the file name is just fine.

1 like
ohffs's avatar

I tend to try and split up files a bit. Mostly to save a single directory ending up with 1000's & thousands of files which can make things quite slow. But that's only a concern if you're letting people upload/change lots of file. But yeah - you need to be a bit careful tying to specific user/category/whatever id's. Maybe you could just do a basic separation like :

/front/$random_filename
/side/$random_filename
/back/$random_filename

I guess it depends on how likely you think it is the category_id etc might change though (or any other random thing you might get asked to do down the line!). It's nice being able to do something like :

/front/2/3/image.jpg

Then from a filesystem level and your app level it's easy to know the file. Especially handy if a user says 'can I have yesterdays file back?' without needing to dig into the db as much to figure out filenames :-)

Please or to participate in this conversation.