avatar upload problem

Post here is you are having problems with any of the tutorials.
Post Reply
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

avatar upload problem

Post by gt3000 »

Hi,I have a problem,I cant see images in 'uploads/avatars',so when I press Submit,the page refreshes quickly and thats all.No images in folders.What should I do to make it upload user avatar?I will post my codes here,hope for help..S.O.S

edit_profile.php

[syntax=php] if (isset($_POST["car"],$_POST["first_name"],$_POST["about"])){
$errors = array();
}
if (empty($_FILES["avatar"]["tmp_name"]) === false){
$file_ext = end(explode('.', $_FILES["avatar"]["name"]));
if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false) {
$errors[]='blablabla';
}

if (empty($errors)){
set_profile_info($_POST["car"],$_POST["first_name"],$_POST["about"], (empty($_FILES["avatar"]["tmp_name"])) ? false : $_FILES["avatar"]["tmp_name"]);
}
$user_info = array(
'car' =>htmlspecialchars($_POST["car"],ENT_QUOTES, 'cp1251' ),
'first_name' =>htmlspecialchars($_POST["first_name"],ENT_QUOTES, 'cp1251'),
'about' =>htmlspecialchars($_POST["about"],ENT_QUOTES, 'cp1251')
);

}else{
$user_info = fetch_user_info($_SESSION["user_id"]);
}
?>
<?php
if (isset($errors) === false){
echo'blablabla';
}else if (empty($errors)){
echo'Your profile updated';
}else{
echo'<ul><li>', implode('</li><li>',$errors),'</li></ul>';
}
?>[/syntax]

and here is function users.php

[syntax=php] //Updates the current profile users info
function set_profile_info($car,$first_name,$about,$avatar){

$car = mysql_real_escape_string(htmlspecialchars($car));
$username = mysql_real_escape_string(nl2br(htmlspecialchars($username)));
$about = mysql_real_escape_string(htmlspecialchars($about));

if (file_exists($avatar)){
$src_size = getimagesize($avatar);

if ($src_size["mime"] === 'image/jpeg'){
$src_img = imagecreatefromjpeg($avatar);
}else if
($src_size["mime"] === 'image/png'){
$src_img = imagecreatefrompng($avatar);
}else if
($src_size["mime"] === 'image/gif'){
$src_img = imagecreatefromgif($avatar);
}else{
$src_img = false;
}

if ($src_img !== false){
$thumb_width = 200;
if($src_size[0]<= $thumb_width){
$thumb = $src_img;
}else{
$new_size[0] = $thumb_width;
$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;

$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
imagejpeg($thumb,"{$GLOBALS["path"]}/uploads/avatars/{$_SESSION["user_id"]}.jpg");
}
}[/syntax]

also I tried to make it by move_uploaded file.but i dont know exactly was it correct or not
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: avatar upload problem

Post by ExtremeGaming »

This makes it much harder to read without the opening <?php tag so here we go for anyone wondering:
[syntax=php]<?php
if (isset($_POST["car"],$_POST["first_name"],$_POST["about"])){
$errors = array();
}
if (empty($_FILES["avatar"]["tmp_name"]) === false){
$file_ext = end(explode('.', $_FILES["avatar"]["name"]));
if (in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false) {
$errors[]='blablabla';
}

if (empty($errors)){
set_profile_info($_POST["car"],$_POST["first_name"],$_POST["about"], (empty($_FILES["avatar"]["tmp_name"])) ? false : $_FILES["avatar"]["tmp_name"]);
}
$user_info = array(
'car' =>htmlspecialchars($_POST["car"],ENT_QUOTES, 'cp1251' ),
'first_name' =>htmlspecialchars($_POST["first_name"],ENT_QUOTES, 'cp1251'),
'about' =>htmlspecialchars($_POST["about"],ENT_QUOTES, 'cp1251')
);

}else{
$user_info = fetch_user_info($_SESSION["user_id"]);
}
?>
<?php
if (isset($errors) === false){
echo'blablabla';
}else if (empty($errors)){
echo'Your profile updated';
}else{
echo'<ul><li>', implode('</li><li>',$errors),'</li></ul>';
}
?>[/syntax]

Now, everything seems okay with edit_profile.php. I'm not sure that file_exists() can check if a temporary file exists (perhaps someone who knows can clarify). Second, you should make sure that your path is correct. You should also note that if the file already exists, it will not upload correctly. You should swap out $_SESSION["user_id"] with something like uniqid();

uniqid()
<?php while(!$succeed = try()); ?>
gt3000
Posts: 11
Joined: Wed Feb 20, 2013 11:46 am

Re: avatar upload problem

Post by gt3000 »

Thanks for the reply!I fixed problem,and also I wanted to ask about one thing,in edit_profile.php there is 'echo'Your profile updated';' and it shows always on this page,you can see all code in my first post.
What should I do to fix it?It meens when you press SUBMIT button,then it gives you notification that ''your profile updated'',but it shows always on this page,when you reload it,open and etc..
ExtremeGaming
Posts: 205
Joined: Mon Jul 09, 2012 11:13 pm

Re: avatar upload problem

Post by ExtremeGaming »

You need to make sure your form has been submitted, so you need to either:

A) Check your post array for your submit button
[syntax=php]<?php
if(isset($_POST['submit_button_name'])) {
// do something
}
?>
<form method="post">
<!-- Make sure you use name="" or your post array will not contain it -->
<input type="submit" name="submit_button_name">
</form>[/syntax]

or B) Since there are issues in IE for submit buttons when you don't click, but hit enter or return, you want to check if "a" form was submitted
[syntax=php]<?php
if($_SERVER['REQUEST_METHOD'] === "POST") {
// do something
}
?>[/syntax]

There is an option C to check if anything else was submitted, but just checking the submission seems like the easiest way.

The advantage of using option A is that you know which form was submitted, however users with IE may have difficulties

The advantage of using option B is that you know no users will have issues, however any form submitted to that page will be considered valid.
<?php while(!$succeed = try()); ?>
Post Reply