Jump to content

[SOLVED] Avatar upload system


alianated

Recommended Posts

Hello again everyone, i dont mean to be a pain but rather than posting this in my other topic i opened a new one just to save confusion, i have a avatar system i have built, it works perfectly on the user registration form users can upload a avatar and it inputs the avatar name into the database and uploads the avatar to the users newly created avatar folder, e.g var/www/html/test_site/includes/avatar/Dave/daves_avatar.jpg <- just a example of the file path, but i have a problem in my edit user page, when i try and upload a new avatar it tells me account edited successfully, it updates the name of the avatar in the DB but it doesnt upload the new avatar in to the users DIR i have spent several hours trying to find the problem but cant im just wondering if any of you guys can help me fish out the baddie in my code :),

 

 

Ill post the working code for the registration page too as they are alike,

 

Registration code..

 

Please Note the registration process works and uploads the avatar into the users newly created DIR.

 

/* Avatar checking */
$field = "avatar"; //Use field name for avatar
$uploaded_dir = "/var/www/html/test/include/avatar/$subuser/";
$avatar_path = $uploaded_dir . $avatar_name;
$width = 100;
$height = 100;
$thumbsize = 100;

if(empty($avatar_name)){
$avatar_name = ereg_replace('$avatar_name','noavatar.png','$avatar_name');
return $avatar_name;
mkdir("/var/www/html/test/include/avatar/$subuser", 0777);
} else {
if(!$avatar_tmpname){
$form->setError($field, "* Avatar does not exist");
} else {
$num_char=strlen($avatar_name);
if($num_char > 20){
$form->setError($field, "* Your Avatar name has $num_char characters, the limit is 20");
} else {
if($avatar_size > 102400) {
$form->setError($field, "* Your avatar is $avatar_size, the maximum upload size is 100kb");
} else if($avatar_type == "image/jpeg"){
mkdir("/var/www/html/test/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

$fileupload = move_uploaded_file($avatar_tmpname, "$avatar_path");
var_dump($fileupload);
} else if($avatar_type == "image/pjpeg"){
mkdir("/var/www/html/test/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

$fileupload = move_uploaded_file($avatar_tmpname, "$avatar_path");
var_dump($fileupload);
} else if($avatar_type == "image/png"){
mkdir("/var/www/html/test/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefrompng($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagepng($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

$fileupload = move_uploaded_file($avatar_tmpname, "$avatar_path");
var_dump($fileupload);
} else if($avatar_type == "image/gif"){
mkdir("/var/www/html/test/include/avatar/$subuser", 0777);

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromgif($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagegif($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

$fileupload = move_uploaded_file($avatar_tmpname, "$avatar_path");
var_dump($fileupload);
} else {
$form->setError($field, "* $avatar_type is wrong extension");
}
}
}
}

 

 

Edit user information page, were the avatar updates in the database but doesnt uplaod a new avatar into the users DIR.

 

/* Avatar checking */
$field = "avatar"; //Use field name for avatar
$uploaded_dir = "/var/www/html/test/include/avatar/$this->username/"; 
$avatar_path = $uploaded_dir . $avatar_name;
$query = $database->getUserInfo($_SESSION['username']);
$avatar_name = $query['avatar'];
$noavatar="noavatar.png";
$myFile = "/var/www/html/test/include/avatar/$this->username/$avatar_name"; 
$width = 100;
$height = 100;
$thumbsize = 100;

if(empty($avatar_name)){
} else {
if(!$avatar_tmpname){
$form->setError($field, "* Avatar does not exist");
} else {
$num_char=strlen($avatar_name);
if($num_char > 20){
$form->setError($field, "* Your Avatar name has $num_char characters, the limit is 20");
} else {
if($avatar_size > 102400) {
$form->setError($field, "* Your avatar is $avatar_size, the maximum upload size is 100kb");
} else if($avatar_type == "image/jpeg"){
if($avatar_name == $noavatar){
//Delete nothing
} else {
//Delete old avatar
unlink($myFile);
}

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/pjpeg"){
if($avatar_name == $noavatar){
//Delete nothing
} else {
//Delete old avatar
unlink($myFile);
}

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromjpeg($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagejpeg($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/png"){
if($avatar_name == $noavatar){
//Delete nothing
} else {
//Delete old avatar
unlink($myFile);
}

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefrompng($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagepng($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else if($avatar_type == "image/gif"){
if($avatar_name == $noavatar){
//Delete nothing
} else {
//Delete old avatar
unlink($myFile);
}

/* Resize Image */
list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
/* Done Resize Image */

/* Resize Image */
$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
$image = imagecreatefromgif($avatar_tmpname);
imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

imagegif($image_p, $avatar_tmpname, 100);
/* Done Resize Image */

move_uploaded_file($avatar_tmpname, "$avatar_path");
} else {
$form->setError($field, "* $avatar_type is wrong extension");
}
}
}
}

 

the only other part that deals with changing the users avatar is this and this just basicly tells the whole script to update the avatar,

 

/* Change Avatar */
if(empty($avatar_name)){
} else if($avatar_name){
$database->updateUserField($this->username,"avatar",$avatar_name);
}

 

Any ideas are greatly appretiated, and once again sorry for bombarding you on my first day.

 

Dave.

Link to comment
Share on other sites

The thing that confuses me here is that if i replace $this->username with $session->username it uploads the image to the /avatar/ DIR and doesnt put it into /avatar/username/ DIR, but even still that is progress but i fail to see why it does update the database ? has anyone got any ideas.

 

Dave.

Link to comment
Share on other sites

Do not store it in the database. For the love of sweet merciful God, do not store it in the database.

 

  • Upload file to server.
  • Record file location in database.

When replacing an avatar.

  • Upload new file to server.
  • Get directory of old avatar from database.
  • Delete old avatar using unlink().
  • Update database field to record file location of new avatar.
     

Link to comment
Share on other sites

Sorry for my bad explanation, the image doesn't store in the databse the image uploads to the users Folder, and records it in the database so that my code to display avatars go look in the database and say ok user x has avatar x, everything works fine with my registration process for adding a avatar it creates the users DIR uploads the image to the users DIR and then stores the name of the avatar into the database, it also displays the users avatar in there profile, im just really sctruggling with getting it to change the users avatar, it should perform 3 tasks, upload new avatar, delete old avatar, store the name of the new avatar in the DB but it doesn't do any of the 3 tasks, as said in my last post i can get it to uplaod to the /avatar/ folder thats all but this is wrong it should be going into /avatar/users file..

Link to comment
Share on other sites

Problem solved i used $ava_name for the deleting of old avatars as i was already using $avatar_name for the uploading of new avatars, i accidently did this

 

$myFile = "/var/www/html/test/include/avatar/$this->username/$avatar_name";

 

Rather than

 

$myFile = "/var/www/html/test/include/avatar/$this->username/$ava_name";

 

So it was trying to delete what was being uploaded, took me a while to notice my little error, 'Doh learning php is fun lol, thank to all that helped and big thanks to Zhadus for spending several hours helping me try to pin point the problem and explaining things about php i did not know XD

 

Dave,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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