Uploading files with PHP does not recognize

1

I'm trying to upload images with PHP and Ajax but the result says it's an undefined value

Javascript code

$('form[name="update-user"]').submit(function(){
        form = $(this);
        $.ajax({
            url: 'switch/painel.php',
            data: form.serialize() + '&acao=update_user',
            type: 'POST',
            beforeSend: function(){
                form.find('.load').fadeIn('fast');
            },
            success: function(resp){
                alert(resp);
            },
            complete: function(){
                form.find('.load').fadeOut('fast')
            }
        });

        return false;
    });

Follow the page code

    <form enctype="multipart/form-data" action="#" name="update-user" class="form-horizontal" accept="image/*" method="POST">
         <input type="file" name="user-profile" id="file-profile">
         <input type="submit" class="btn btn-success" value="Salvar dados">
    </form>

Controller file

case 'update_user':
    var_dump($_FILES['user-profile']);
break;

Result: undefined index: user-profile

    
asked by anonymous 17.03.2017 / 23:09

1 answer

0

To send files with ajax I recommend using jquery-forms ( link ) saved me a lot of time and support in many browsers.

If you do not want to use jquery or plugins, I've managed to get an easier but more restrictive approach to newer browsers with html5 and the formData object ( link ) in this link is well explained how to do even with php.

And finalizing instead of doing:

var_dump($_FILES['user-profile']);

try doing:

var_dump($_FILES);

So you can see if there is anything in the array that might have a different name, I hope I have helped.

    
17.03.2017 / 23:41