Jump to content

Storing User Profile Image in Mysql Databse Table


Recommended Posts

Hey guys,


i've been searching the web trying to figure out the best method of storing images in a database table when it comes to user registration. Basically, users will register with only 3 elements: Upload Image( turned into Avatar/Thumbnail), E-mail, and Password.


Now i've figured out the basics on how to store user information such as the e-mail and pass, but I still haven't found any direct answers on the topic of properly managing user avatars/thumbnails. Now these avatars will be displayed everytime the user posts, much like any forum or social network. 


So, do I store the image itself in the database? Do I store it in a user_avatar file/directory on the server and have a direct link in the table to fetch and display the image? What would be the best method to go about this? I thought about maybe having a table with user_ID, avatar_ID and avatar_link(direct link to image).

Edited by DomMarx
Link to comment
Share on other sites

Don't store images directly in the database. Store them in a directory on the server, then store the filename in the database. The path should be the same for all images, so it is redundant to store that information; plus, if you change servers or move the folder, you will have to update every row in the table.

Link to comment
Share on other sites

Hi DavidAM,


Thanks for the help. I believe I understand what you're saying, except if I shouldn't store images directly in the database, or store the direct path to the images in every single row for the obvious reason you pointed out, where do I place the path? 

Link to comment
Share on other sites

Kind of left that out, didn't I. I would hardcode the path as either a configuration option of the application, or as a defined constant in a globally included file. Don't hardcode the value everywhere it is used, since it would have to be changed in multiple places.


define('C_IMAGE_PATH_HTML', '/images/');
define('C_IMAGE_PATH_PHP',  '/home/mad/www/images/');
something like that defines "images" as a subdirectory of your public directory for adding it to IMG tags; and the absolute path for PHP statements (such as move_uploaded_file). Obviously, you might have to change both of them when changing hosts, or if you move your image folder.


# Use C_IMAGE_PATH_PHP for PHP access to the filesystem
move_uploaded_file($_FILES['tmp_name'], C_IMAGE_PATH_PHP . $imageFileNameInDatabase);

# Use C_IMAGE_PATH_HTML for HTML access to the images
echo '<IMG src="' . C_IMAGE_PATH_HTML . $imageFileNameInDatabase . '">';
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.