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 关于#Mybatis#的问题:怎么办重复数据如下"KSDM":37,"KSMC":"呼吸内科门诊","ksdm":37,"ksmc":"呼吸内科门诊"xml文件填写
  • ¥40 servlet的web程序部署出错
  • ¥50 activiti 新建流程系列问题
  • ¥50 为什么我版本升级之后运行速度变慢5倍??
  • ¥15 如何在gazebo中加载机械臂和机械手
  • ¥15 纯运放实现隔离采样方案设计
  • ¥20 easyconnect无法连接后缀带.com的网站,一直显示找不到服务器ip地址,但是带有数字的网站又是可以连接上的,如何解决
  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!