dongzhang6021 2016-09-11 12:31
浏览 64
已采纳

无法在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.

  • 写回答

1条回答 默认 最新

  • dtevhgk028372 2016-09-11 12:35
    关注

    Try with this:

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

    NB: Answer in the comments

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)