0

I'm creating an avatar upload and I'm passing the file name through JQuery where I then want to process and resize it in an external php file and send the path to a database. I've created the form and when I send it adds the path to the database but will not recognise that a file has been uploaded in the initial php. Here is my code for getting the file name and running it through php:

The code on the profile page:

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");
                            }
                            });
                        }

The code on "php/update-user-profile.php":

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

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

How can I get it to recognise $avatar as a legitimate file?

user2200771
  • 11
  • 1
  • 5

1 Answers1

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"].

mkjasinski
  • 3,115
  • 2
  • 22
  • 21
  • The file name has been passed through and is displayed as $avatar but it still isn't recognising it without the quotes – user2200771 Mar 24 '13 at 10:05
  • method of action is `POST`? `var_dump($_FILES, $_POST['avatar']);` please. – mkjasinski Mar 24 '13 at 10:11
  • And running dump displays 1array(0) { } NULL – user2200771 Mar 24 '13 at 10:14
  • maybe this: good and supported tool [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin). – mkjasinski Mar 24 '13 at 10:19
  • Thanks but I was hoping to do this without too many plugins, etc. The file name is passing through no problem but it just will not display in the $_FILES section? – user2200771 Mar 24 '13 at 10:26
  • @user2200771 because you send only data in post. – mkjasinski Mar 24 '13 at 10:31
  • @user2200771 look here: [http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery). – mkjasinski Mar 24 '13 at 10:33
  • Thanks but I'm sorry, I understand the problem but not the solution. I'm using JQuery post and not an Ajax function - Isn't there an easier way to send the data without having to use 2 different function types? – user2200771 Mar 24 '13 at 10:45