DoeJohn's avatar

What is your "upload strategy"? (directory structure & naming uploaded images)

When a user uploads an image to your site - what is your upload / storage strategy?

For example, lets say that users can upload:

  • Avatars (profile images)
  • Images in their forum posts
  • Images in their articles

1. How would you organize the directory structure?

Would you just have three sub-directories in public/images (or maybe in public/uploads?):

  • One for avatars: public/images/avatars (or public/uploads/avatars or public/images/uploads/avatars?)
  • One for forum posts: public/images/forum
  • One for articles: public/images/articles

?

Or you would just have one public/uploads directory and put everything there, no matter if it's an avatar or uploaded image for forum post/article?

Or, for example, you would create username subfolders in the uploads directory... ?

2. Would you put all of the uploaded files (images) inside one directory? (for example, all uploaded avatars would be inside one directory)

If yes, then you could end up with thousands (or even hundreds of thousands) images (avatars) inside one directory and, as I understand it, this could be bad for many reasons.

If no, then what approach would you use? I know these two ways:

3. Finally, how would you name uploaded images (avatas, forum post images, article images...)?

0 likes
2 replies
martinbean's avatar

you could end up with thousands (or even hundreds of thousands) images (avatars) inside one directory and, as I understand it, this could be bad for many reasons.

@DoeJohn What reasons?

Personally, I just have an uploads directory (in an S3 bucket). I use Laravel’s store() method on an UploadedFile instance to move it, and then store that path in an Image model. This model can then be attached to other models via an Eloquent relationship.

Tarasovych's avatar

For the first question, I'd rather have three separate folders. It's easy to maintain. For example, you want to move some of your images to another server. Just move folder you need, don't search for certain files.

Please or to participate in this conversation.