PHP文件上传错误的情况 [英] PHP file upload error conditions

查看:135
本文介绍了PHP文件上传错误的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个配置文件编辑设置。似乎只有在图像上传时才能编辑信息。我发现允许错误消息是一个条件允许一些更多的操作,所以我现在尝试它,我的条件声明不工作,因为它应该。

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

print_r($ _ FILES ['files'] ['error']);
回显if;

foreach($ _ FILES ['files'] ['name'] as $ file => $ name){
$ filename = $ name;
$ b尝试{
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 ]);
回声Elseif; (b












$ b $ $ b 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']);
echoelse;

code










$ b $ p


$ b

当我检查发送数组时,它是正确的,但错误的条件,即:无论文件检查,它都会运行else语句。

我的问题:



我的代码有什么问题,除了任何安全或效率的缺陷?

解决方案

$ _ FILES ['files'] ['error']与文件数组一起返回错误代码。有不同类型的错误代码,所有的代码在下面的链接中提到细节:

请检查


print_r($ _ FILES ['files'])

看看你有什么回应。当你发布你的数组响应时,你可以通过$ _FILES ['files'] ['error'] [0]得到错误代码,或者像上面提到的那样使用switch case以下链接。



详情请看这里:
http://php.net/manual/en/features.file-upload.errors.php



关于调试,总是从上到下逐步调试代码。检查$ _POST,$ _FILES,$ _SERVER等详细信息,如果您遇到与数据处理有关的特定问题。


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?

解决方案

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

这篇关于PHP文件上传错误的情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆