drwn65609
drwn65609
2017-04-14 18:19

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 drn9573 4年前

    $_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.

    点赞 评论 复制链接分享

相关推荐