0

havent tackled file upload before but having an initial issue.

I already have a form that updates user preferences (email and a few tick boxes for options).

This works perfectly fine but am now adding profile images into the form.

The form looks like so:

<form class="updatepref" method="post" enctype="multipart/form-data">

    <input type="email" name="email">        

    <input type="file" name="upfile">

    <input type="submit" class="update" Value="Save Settings">

</form>

I am posting this data via ajax:

$('.updatepref').submit(function(){
  $.ajax({
    type: "POST",
    url: "../process/updateuserpref.php",
    data: $('.updatepref').serialize(),
    dataType: "json",
    success: function(response){            
    }
  });
return false;
});

And then the start of the PHP page for the file upload is:

// Undefined | Multiple Files | $_FILES Corruption Attack
// If this request falls under any of them, treat it invalid.
if (
    !isset($_FILES['upfile']['error']) ||
    is_array($_FILES['upfile']['error'])
) {
    throw new RuntimeException('Invalid parameters.');
}

No matter what, i always get the 'Invalid Parameters' error message.

Whats going on? Can ajax serialise file upload?

When i check in the parametres sent (using firebug) it only show the email and any tick boxes etc. The file is not posted.

Lovelock
  • 7,689
  • 19
  • 86
  • 186
  • Is a big file? The default timeout to upload I think is 30 secs – dhalfageme Jul 11 '14 at 10:17
  • Just updated the question, the file is not being posted at all. If checking the params sent it just shows everything else in the form that sent but nothing with the file – Lovelock Jul 11 '14 at 10:20
  • But why are you using "!" operator in the condition of the if?? If there is no error the condition will be true and RuntimeException will be thrown, no? – dhalfageme Jul 11 '14 at 10:22
  • as far as i know you can't upload a file using just `Ajax`. you can't `POST` the file-content with `Ajax`, cause you don't have access to the file-content itself with javascript. it's quite a tricky thing to do, that's why tools like [jQuery File Upload](http://blueimp.github.io/jQuery-File-Upload/) or [Malsup's Method with the jQuery form plugin](http://malsup.com/jquery/form/#file-upload) exist and are quite popular. (i personally prefer malsup's method) – low_rents Jul 11 '14 at 10:33
  • you must use form data. – Arun Jul 11 '14 at 10:45

0 Answers0