Uploading files with PHP does not recognize

Asked

Viewed 136 times

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 code of the page

    <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;

Upshot: Undefined index: user-profile

1 answer

0


To send files with ajax recommend you use jquery-Forms (http://malsup.com/jquery/form/) saved me a lot of time and support in many browsers.

If you do not want to use jquery or plugins, I have already managed in an easy but restrictive way to newer browsers with Html5 and the formData object (https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects) this link is well explained how to do even with php.

And finishing instead of doing:

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

try to do:

var_dump($_FILES);

So you will be able to see if there is anything in the array perhaps with another name, I hope to have helped.

  • 1

    We appreciate your willingness to collaborate with the community, but it might be interesting to read the text We want answers that contain only links? The main problem is that if the answer link eventually ceases to exist, the answer completely loses its meaning. To get around this, check with the content of the link the main points that are valid to the question and describe them here, keeping the link only as a support material.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.