eric01
eric01

Reputation: 919

Why is PHP not seeing the file upload through POST and jquery?

I have this HTML:

<form action='uploadhandle.php' method='POST' enctype="multipart/form-data">
<input type='file' class='fileinput' id='photo1' name='photo1'>
<input type='button' id='upload1' name='upload1' value='Upload'>
</form>

My jquery code is:

$('#upload1').click(function(){
    $.ajax({
        url: "uploadhandle.php",
        data: 'photo1='+photo1,
        success: function(data){$('#result_div').html(data)}

        });

In my uploadhandle.php, when I try to display the $_POST['photo1'], nothing comes up, it's "undefined". Does anyone know what I did wrong?

Thanks a lot, Regards

Upvotes: 0

Views: 166

Answers (2)

Linus Thiel
Linus Thiel

Reputation: 39261

You can't upload file data through XHR (i.e. ajax). There is a file upload API in XHR2, but the most practical and cross browser compatible way today seems to use a hidden iframe to do the file upload.

Edit: See How do you post to an iframe?

Upvotes: 0

Tim Withers
Tim Withers

Reputation: 12069

You cannot upload a file via AJAX. It isn't possible.

What IS possible is using a plugin or another method that "simulates" ajax by creating an iframe and submitting the info in the background. There are several plugins that handle this, some are very complex, some simply extend the ajax function itself.

That being said, your syntax server side is also wrong. You have to deal with $_FILES not $_POST to find and use the submitted file.

Good luck.

Upvotes: 6

Related Questions