Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jackie Jen
Jackie Jen
2,723 Points

storing image in database compare with storing at the folder server

Hi,

Is anyone can explain to me which is the better way to store image in database mysql in binary format compare to store image by upload in specific server folder and store the path in database?

Thanks

1 Answer

Devron Baldwin
Devron Baldwin
3,508 Points

Hi Jackie Jen

Generally databases are best for data and the file system is best for files. It depends what you're planning to do with the image though.

If you're storing images for a web page then it's best to store them as a file on the server. The web server will very quickly find an image file and send it to a visitor. Sending files to visitors is the main job of a web server.

If you were to store the same image in a database then the amount of steps to get to this image is greatly increased so the image will be slower to download. Also it will use up more server resources. The web server will have to connect to the database and then query the database to get the image, download the image from the database and then send the visitor the image.

This will also tie up the database a little bit while it sends the image. It might not be a problem if your web site is not high traffic but as soon as you start getting a lot of visitors then you'll quickly notice the slowness. Storing files isn't really what databases were made for even though they can do it.

There are sometimes good reasons to store images in a database but there aren't too many. Some uses I've seen for storing images in a database are things like sensitive images such as a photo of an employee's or customer's face that will never be used externally. In this case the database gives you a way to tightly control access to the images.

I hope that helps!