Jump to content

Storing User Profile Image in Mysql Databse Table


DomMarx

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).

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.

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? 

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 . '">';

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.