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

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

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常