0

我正在创建头像上传,并通过 JQuery 传递文件名,然后我想在外部 php 文件中处理和调整它的大小并将路径发送到数据库。我已经创建了表单,当我发送它时,它添加了数据库的路径,但不会识别出文件已在初始 php.ini 中上传。这是我获取文件名并通过 php 运行它的代码:

个人资料页面上的代码:

var avatar=$("#avatar").val();
                        var name=$("#name").val();
                        var location=$("#location").val();
                        var about=$("#about").val();
                        var visible = ( $("#visible").is(":checked") ) ? "checked" : "not checked";

                        if(nameok == true || name.value > 2)
                        {           
                            $('.userValidation').html("Processing").removeClass("error").addClass("success");
                            jQuery.post("php/update-user-profile.php", {
                            avatar:avatar,
                            name:name,
                            location:location,
                            about:about,
                            checked:visible
                            },  function(data, textStatus){
                            if(data == 1){
                                $('.userValidation').html("Updated Successfully").removeClass("error").addClass("success");
                                window.location = 'home.php';
                            }
                            else{
                                $('.userValidation').html("Something's Wrong, Please Try Again").removeClass("success").addClass("error");
                            }
                            });
                        }

“php/update-user-profile.php”上的代码:

//Avatar Preferences
$avatar = mysqli_real_escape_string($con, $_REQUEST["avatar"]);

$fileName = $_FILES["$avatar"]["name"]; // The file name

如何让它将 $avatar 识别为合法文件?

4

1 回答 1

0

Foe example: structure of $_FILES array:

Array
(
    [file] => Array
        (
            [name] => file.ext
            [type] => text/html
            [tmp_name] => /temp/ng736x.tmp
            [error] => 0
            [size] => 644563
        )
)

if $avatar variable is name of your field on form, that the name of file in $_FILES is $fileName = $_FILES[$avatar]["name"], but name of your file input on form is avatar that name of file is $fileName = $_FILES['avatar']["name"].

于 2013-03-24T10:03:09.790 回答