Reputation: 11
I want to upload a avatar for a user when he she registers or in his/her profile? What I understand is that you need to "save" the image in a location path and then call the img's name when you want to display it??
Must I change my users table in order to have a unique pic associated with every user registered? This is my current users table...
+-------------------------------------------------------------------------+
| users |
+---------+-------------+-----------+------------+-----------+------------+
| user_id | user_name | user_pass | user_email | user_date | user_level |
+---------+-------------+-----------+------------+-----------+------------+
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+---------+-------------+-----------+------------+-----------+------------+
Must i add a extra column for every user's pro file pic? AND HOW WILL THAT LOOK?
Where is the best place to upload the image? In the user's profile or during registration? This is the code I want to add in either the user's profile or during registration... I have this simple form.
<form action="picUpload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename: </label>
<input type="file" name="file" id="file"/>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
then picUpload.php
<?php
if((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 50000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return code: " . $_FILES["file"]["error"] . "<br/>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br/>";
echo "Type: " . $_FILES["file"]["type"] . "<br/>";
echo "Size: " . $_FILES["file"]["size"] / 1024 . " Kb<br/>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br/>";
if(file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists!";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file!";
}
?>
How do one put this in the database and how do I call it in the php file??
Thanks!
Upvotes: 0
Views: 3017
Reputation: 141
to clear things up you just store the pic path in the database not the actual picture.
Upvotes: 0
Reputation: 1751
Make the user images accessible by their user id's.
Also, Consider Using PHP GD to resize images, and keep a cache of the resized images.
Upvotes: 1
Reputation: 4995
Save the name of $_FILES["file"]["name"]
and save it in user_profile_pic(a new column in the table). If you don't want to modify this table, you can create a new table that has a user_id foriegn key to this table and and user_profile_pic column there instead.
Upvotes: 2
Reputation: 3689
Just create a seperate field in your DB for user_pic and store the path of the uploaded file in there. When you want to display it, just set the images' href to the file in the database. And yes of course, you'll need to have a seperate column for it, that's the most logic and easiest way!
So your DB is then like:
+--------------------------------------------------------------------------------------+
| users | |
+---------+-------------+-----------+------------+-----------+------------+------------+
| user_id | user_name | user_pass | user_email | user_date | user_level | user_pic |
+---------+-------------+-----------+------------+-----------+------------+------------+
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+---------+-------------+-----------+------------+-----------+------------+------------+
To store something in a database, you'll need to query your SQL. That's different on the type of SQL you have though! Same again when you want to call the filename. But you should know that from the other processes you do when you create a new user or output something of a user on a page!
Upvotes: 0