DomMarx Posted June 24, 2013 Share Posted June 24, 2013 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). Link to comment https://forums.phpfreaks.com/topic/279522-storing-user-profile-image-in-mysql-databse-table/ 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. Link to comment https://forums.phpfreaks.com/topic/279522-storing-user-profile-image-in-mysql-databse-table/#findComment-1437693 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? Link to comment https://forums.phpfreaks.com/topic/279522-storing-user-profile-image-in-mysql-databse-table/#findComment-1437784 Share on other sites More sharing options...
DomMarx Posted June 25, 2013 Author Share Posted June 25, 2013 nevermind! I think I got it! Link to comment https://forums.phpfreaks.com/topic/279522-storing-user-profile-image-in-mysql-databse-table/#findComment-1437786 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 . '">'; Link to comment https://forums.phpfreaks.com/topic/279522-storing-user-profile-image-in-mysql-databse-table/#findComment-1437830 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.