Have an account? Sign in
Login  Register  Facebook
How to add user profile image to database
I am working on a profile page for users and would like to give them the option of adding an avatar or profile image. The code I am using is not working. Images are not being moved into the tmp folder named "avatars." Not sure how to fix! Here is my code:
<?php
include("functions/globals.php"); 
include("databasefunctions.php");
include("functions/userfunctions.php"); 
?>

<?php session_start(); ?>

<?php
if($_SESSION['user'])
{?>
                      
Welcome: <?php echo $_SESSION['user']->username; ?>
                <?php
                      }?>
                      
                      
<?php

if($_POST['submit'] == 'Upload')
{
//get file attributes
$name = $_FILES['avatar']['name'];
$tmp_name = $_FILES['avatar']['tmp_name'];
$location = "avatars/$name";

if ($name)
{
//start upload process


move_uploaded_file($tmp_name,$location);

$result = mysql_query("UPDATE users SET imagelocation='$location' WHERE username='$username'");

die("Your avatar has been uploaded <a href='view.php'>Home</a>");
}
	else
	if(!$name)
$error = "Please select an image";

}


?>
<form action='profile.php' method='post' enctype='multipart/form-data'>
Upload:<input type='file' name='avatar'><input type='submit' name='submit' value='Upload'>
</form>
I also added this before move_uploaded_file:
echo '<pre>';
print_r($_FILES);
echo '</pre>';


This is what returned:

Array
(
[avatar] => Array
(
[name] => me.jpg
[type] => image/jpeg
[tmp_name] => /var/tmp/phpFZKL9d
[error] => 0
[size] => 35433
)

)

Any ideas???
Started: November 9, 2011 Latest Activity: November 10, 2011 php profileimages profile

can you explan "what you see when click upload"
November 9, 2011

When I click upload I always get the message "your avatar has been uploaded. But when I go to the "avatars" folder, nothing is there. And of course, nothing is in the database either. The database variable is "imagelocation" by the way.
November 9, 2011

be sure you location file is 0777 chmod
November 10, 2011

1 Answer
thanks. not sure how to do that? I have created a users table and users should be able to update the table by uploading an avatar on their profile page.

this is what I added to the beginning of profile.php

chmod("/avatars/avatar", 0777); // octal; correct value of mode

this doesn't look correct to me:( if you have time, can you send me the correct code? are you sure it's not 0755?

Thanks so much for your help!

Posted: johnnymd
In: November 10, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer