Déven Esteve Déven Esteve - 2 months ago 13
PHP Question

Cannot upload file to server in PHP

I'm trying to create a profile page for my members that allows them to change their avatar, but I'm having some issues with the upload part.

if ($_POST['avatar']) {
$avatar = $_POST['avatar'];
$avatype = $_FILES['avatar']['type'];
$avasize = $_FILES['avatar']['size'];
$avatemp = $_FILES['avatar']['tmp_name']; //edited, removed the e in tmp
$avaname = $_SESSION['login'];
if ($avasize > 512000) {
echo 'too big';
} else {
$avapath = 'images/avatar/'.$avaname;
if (is_uploaded_file($avatemp)) {
if (move_uploaded_file($avatemp, $avapath)) {
$avaIsSet = $db->query('UPDATE `users` SET `hasavatar`="1" WHERE `id`="'.$_SESSION['id'].'"');
$avaIsSet->execute();
} else {
echo 'not moved';
}
} else {
echo 'not uploaded';
}
}
}


The fact is that when I'm trying to get it to work, I always have the same error code:
not uploaded


I really hope you guys will be able to help me,

Thanks

EDIT:

Here is the html code that leads to the php file which applies the modifications:

<?php if ($selfmodify) { ?>
<form action="updateprofile.php" method="post">
<div class="form-group">
<? } ?>
<div class="col-md-2 col-sm-2"></div>
<div class="col-md-3 col-sm-3" id="avatar">
<br/>
<img src="images/avatar/<?php if ($pdata[hasavatar] == 1) { echo $pdata[id]; } else { echo "default"; } ?>" style="width: 100%" class="img-rcor">
<?php if ($selfmodify) { ?>
<input type="file" name="avatar" accept="image/*">
<? } ?>
</div>
<div class="col-md-1 col-sm-1"></div>
<div class="col-md-4 col-sm-4">
<h3><?php echo $pdata[login]; ?></h3>
<br/>
<p align="left">Prénom: <?php if ($selfmodify) echo '<input type="text" class="form-control" name="firstname" value="'; ?><?php echo $pdata[fname]; ?><?php if ($selfmodify) echo '">'; ?><br/><br/>
Nom: <?php if ($selfmodify) echo '<input type="text" class="form-control" name="lastname" value="'; ?><?php echo $pdata[famname]; ?><?php if ($selfmodify) echo '">'; ?><br/><br/>
<?php if (($pdata[showmail] == 1) || ($selfmodify)) echo 'E-mail: '.$pdata[email]. '<br/>'; if ((!$selfmodify) && ($pdata[showmail] == 1)) echo '<br/>'; ?>
<?php if ($selfmodify) { ?>
<input type="checkbox" name="showmail" value="showmail" <?php if ($pdata[showmail] == 1) echo 'checked'; ?>> Rendre mon e-mail visible
<br/><br/>
<? } ?>
Dernière connexion: <?php if ($pdata[isonline] == 1) { echo 'Actuellement en ligne'; } else { echo $pldate.' à '.substr($pdata[lasttime], 0, -3).' (GMT)'; } ?></p>
<br/>
<?php if ($selfmodify) { ?>
<br/><br/>
<input type="submit" class="btn btn-info" name="confirm" value="Confirmer">
</div>
</form>


I know that my code is a real mess, but I'm focusing on the result that it gives, not the way my code is structured. I'm unexperienced, I guess it will come later, but for now I'm trying my best.

Answer

Try with this:

$avatemp = $_FILES['avatar']['tmp_name'];

NB: Answer in the comments

Comments