3

I found all of these answers to a question I had:

Store pictures as files or in the database for a web app?

Storing images in SQL Server?

To Do or Not to Do: Store Images in a Database

And the last one links to even more versions of the same question. They typically have the same answer, suggesting to use a file system, then store the address in a data base. When they say "file system", do they mean store it in the folders that you use to make the website?

The way I've been learning to make websites, there's the "views" folder that has all the layouts, then there's the "public" folder that stores your css and js files. Then I have a line of code in the app so I don't have to type public all the time when I'm linking to my css files or js files. So would I store the images in there? With it's own folder and maybe a folder for each user? What happens when there's a lot of users and a lot of pictures?

Or should the pictures go somewhere else entirely?

I've done my best to find the answer on my own, but "file system" is such a generic term, I can't find the specific answer.

Community
  • 1
  • 1
Cordello
  • 136
  • 10

1 Answers1

1

Yes, filesystem means to put them as files alongside the files that your website consists of.

Generally, there are a couple good practices to follow when doing so:

  • Put them in some folder that's dedicated to user data, not to the same folder you put your website's core files
  • Generate artificial file names (UUIDs or database-generated IDs) for them instead of using the original file names to avoid name collisions
  • If you expect a lot of files, on some filesystems it may be a good practice to create a level of subdirectories to limit number of items in each directory
  • It might be a good idea (depending on your use case) to forbid direct access to this folder with user data through plain HTML
    • Instead read the files and output them through your script
    • This is required if the files are not all public (if any authorization is needed to see them)
Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43