1

I am trying to upload files to uploads folder with this code: Upload multiple image using AJAX, PHP and jQuery

In this post, what is the correct way to upload the files to uploads folder

This is what i have but doesn't upload the files:

if (isset($_POST["file"])) {
// do php stuff
move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name']);
// call `json_encode` on `file` object
$file = json_encode($_POST["file"]);

// return `file` as `json` string
echo $file;
};

The php code is in the same file at the top where also the form and drop area is

Community
  • 1
  • 1
Jack Maessen
  • 1,780
  • 4
  • 19
  • 51

2 Answers2

0
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
 if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
      // return json;
    } else {
        echo $_FILES['userfile']['error']; // only just for debuging
        // return json;
    }
shivani parmar
  • 314
  • 2
  • 17
0

your php code is coorect but you have to change in your ajax call

var formData = new FormData($("#formId"));

  $.ajax({
        url:url,
        method : 'POST',
        dataType:'json',
        data: formData,
        mimeType: "multipart/form-data",
        contentType: false,
        processData: false,
        success:function(data){

} });

sunilwananje
  • 714
  • 5
  • 17