Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

christiangerdes's avatar

Should I store images in db, folder or use other services?

Hi everyone,

I have a question. What would be the best way to store images? In mysql, filesystem or another service?

I've researched this question many of times and every time people respond - It just depends on the scale of your application and the structure.

So.... Let me start being specific. In this case we're talking about www.tilsalg.dk. It's a free market place for everyone, who want to either sell or buy stuff. Like ebay :D As you probably guess every item can have multiple associated images (up to 6). Users are allowed to rotate images and change the order of the way they're listed. Currently the images are stored in a mysql-db with both a full version of the image and a thumbnail to show in search resultats.

Can you help me answer; - What is best practice for my application and why?

Please keep in mind, I run a low budget and use this project as a learning curve :D

Thanks!

0 likes
12 replies
chrisgeary92's avatar

Personally I would store them in the filesystem and just store the URL within the database. Typically database storage is more expensive. Unless you have good reason for storing them in the database (storing versions of the image or storing an image that changes dynamically) I would use the filesystem.

pmall's avatar

I don't like to store files in the db, and storing them in the filesystem is a nightmare to manage.

If I had a live project like yours I would try some cloud file storage like S3 or Cloudinary. It cost some money but the amount of headache handled worth it.

By the way, if someone know something like a local S3 like server, please let me know it would be awesome. I mean some kind of local db made to store files with an unique hash or something.

pstephan1187's avatar

I would use Amazon's S3 and use a file system class (like FlySystem) to easily manage those images. The reason I would use S3 is because you don't need to worry about space and Amazon automatically takes care of redundancy for you. It keeps your filesystem clean and won't bog down the harddrive of your server.

rikh's avatar

I would not store the binary data in the database, especially if you plan to serve these images to a browser at some point.

I'd store them on something like Amazon S3 and just store the URL in the DB. You can serve the images directly from S3 and even set it up as a CDN. Storing them in the local filesystem of the server limits your ability to scale the application past 1 server, so I would not recommend that.

1 like
christiangerdes's avatar

Thank you all!

So if I went over and began using S3, would I have to upload both the full version and the thumbnail - or should i just generate the thumbnail when it's requested?

pmall's avatar

You should upload the thumbnail too. It is wasted server resource to generate a thumbnail from the original each time you want to display it.

pstephan1187's avatar

I would just upload the original image and generate the thumbnail dynamically. Cache the thumbnail after it's generated so you don't have to keep generating it. That will keep you from having to keep track of both the the original and thumbnail versions of the image, and you won't have to generate the thumbnail on every request.

1 like
christiangerdes's avatar

Alright, so... Finally I can hear that storing images (in my case) in db aren't the best way to go around it.

Thank you everybody. I appreciate it :D

bashy's avatar
  • Small = filesystem is fine, store URL like chrisgeary92 said. Although this doesn't stop you from using Amazon S3.
  • Large/scaleable = offsite (mentioned above)
pmall's avatar

@threeel Thanks :)

I've seen this before but it seemed hard to install & configure. I'll have a look.

Please or to participate in this conversation.