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

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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵