DomMarx Posted June 24, 2013 Share Posted June 24, 2013 (edited) 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 June 24, 2013 by DomMarx Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 24, 2013 Share Posted June 24, 2013 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. Quote Link to comment Share on other sites More sharing options...
DomMarx Posted June 25, 2013 Author Share Posted June 25, 2013 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? Quote Link to comment Share on other sites More sharing options...
DomMarx Posted June 25, 2013 Author Share Posted June 25, 2013 nevermind! I think I got it! Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 25, 2013 Share Posted June 25, 2013 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 . '">'; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.