occhiso occhiso - 6 months ago 7
MySQL Question

PHP to store images in MySQL or not?

I have built a small web application in PHP where users must first log in. Once they have logged in, I intend on showing a small thumbnail as part of their "profile".

I will have to ensure the image is below a particular size to conserve space, or ensure it is a particular resolution, or both, or even perhaps use something like image magick to scale it down.

Not sure what the best approach for that is yet, any ideas welcome.

Also, I have been trying to work out if it is better to store the image in the

users
table of MySQL as a blob, or maybe a separate
images
table with a unique id, and just store the appropriate image id in the
users
table, or simply save the uploaded file on the server (via an upload page as well) and save the file as
theUsersUniqueUsername.jpg
.
Best option?

I found a tutorial on saving images to mysql here:
http://www.phpriot.com/articles/images-in-mysql

I am only a hobby programmer, and haven't ever done anything like this before, so examples, and/or a lot of detail is greatly appreciated.

Answer

Always depends of context, but usually, I store a user image on the filesystem in a folder called /content/user/{user_id}.jpg and try to bother the database as little as possible.