drwn65609 2017-04-14 18:19
浏览 91
已采纳

PHP文件上传错误情况

I am trying to create a profile editing setup. It seems as though the information is edited only when an image is being uploaded. I found out that allowing the error message to be a condition allows for some more manipulation so I attempted it now my condition statement is not working as it should.

if($_FILES['files']['error']==0) {

    print_r($_FILES['files']['error']);
    echo "if";

    foreach($_FILES['files']['name'] as $file => $name) {
        $filename = $name;

        try{
            if(move_uploaded_file($_FILES['files']['tmp_name'][$file],'uploads/'.$filename)) {
                $updateInfo = $db->prepare("UPDATE users SET image       = :image, aboutme     = :aboutme WHERE id = :id");

                $updateInfo->bindParam(":image", $filename);
                $updateInfo->bindParam(":id", $_SESSION['user']['id']);
                $updateInfo->bindParam(':aboutme', $aboutme);

                $updateInfo->execute(); 
            }
        } catch(Exception $e) {
            echo $e;
        } 
    }
} elseif($_FILES['files']['error'] == 4) {
    print_r($_FILES['files']['error']);
    echo "Elseif";

    try{
        $updateInfo = $db->prepare("
        UPDATE users
        SET 
            aboutme     = :aboutme

            WHERE id = :id
        ");
        $updateInfo->bindParam(':id', $_SESSION['user']['id']);
        $updateInfo->bindParam(':aboutme', $aboutme);


        $updateInfo->execute();
    } catch(Exception $e) {
        echo $e;
    } 
} else{
    print_r($_FILES['files']['error']);
    echo "else";
}

}

When I check what array is being sent, its the correct one but the wrong condition, ie: it would run the else statement no matter the file check.

My question:

Is there something wrong with my code, with the exception of any security or efficiency flaws?

  • 写回答

1条回答 默认 最新

  • drn9573 2017-04-14 19:23
    关注

    $_FILES['files']['error'] returns error code along with the file array. There are different type of error codes, all codes are mentioned in following link with details:

    Please check by

    print_r($_FILES['files'])

    and see what are you getting in response.

    As you posted your array response, you can get error code by $_FILES['files']['error'][0] or use switch case as mentioned in following link.

    See here for more details: http://php.net/manual/en/features.file-upload.errors.php

    Also regarding debugging, always debug code step by step from top to bottom. Check $_POST, $_FILES, $_SERVER etc details if you get some problem particular related to data process.

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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号