2

so i have this problem, i can not possibly read/save files by AJAX/JQuery i`m sending using this

<form class='file'>
 <input type='file' class='file_upload' name='def' required/>
 <input type='submit' value="Wyślij">
</form>

This is my try on the sending JQuery stuff:

$(document).ready(function(){
 $("form.file").unbind().on("submit",function(event){
  $(this).find('input[type=submit]').attr('disabled', 'disabled');
  event.preventDefault();
  $(this).find(".file_upload").each(function(){
   sendFile(this.files[0]);
  });
  
  $(this).parent().remove();
 });
});


function sendFile(file){
 $.ajax({
  type: 'post',
  url:'sayv.php?name='+file.name,
  data:file,
  processData: false,
     contentType: file.type,
  success: function(data){
   console.log(data);
  }
   });
}

And, i can`t possibly read it anyway by the PHP, $_FILES is empty, $_POST too...

Hue Hue
  • 35
  • 4
  • 1
    Are you missing enctype="multipart/form-data" in the form tag? – MrD Jan 28 '15 at 21:05
  • 2
    possible duplicate of [How to upload file through Jquery/AJAX](http://stackoverflow.com/questions/19920322/how-to-upload-file-through-jquery-ajax) – Jhuliano Moreno Jan 28 '15 at 21:06

1 Answers1

0

Since you sending the file bare, that is not wrapped in a form data object, its going to be the request body. use file_get_contents('php://input'); to read it.

file_put_contents('filename.ext', file_get_contents('php://input'));
Musa
  • 96,336
  • 17
  • 118
  • 137