0

i want to create a formless drag and drop file upload using JavaScript's FormData, but PHP doesn't seem to be receiving the file. Am i missing some request headers or something?

JavaScript:

if (item.kind === 'file') 
            {
                const file = item.getAsFile();
                const fileFormData = new FormData();
                fileFormData.append('file', file);
                $.ajax({
                    url: "backend/uploadFiles.php",
                    type: 'POST',
                    data: fileFormData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (returndata) {
                        console.log(returndata);
                    }
                });

PHP:

<?php
var_dump($_POST);
var_dump($_GET);

PHP output:

array(0) {
}
array(0) {

}

2
  • Are you sure your javascript function is executed (item.kind ==='file')? And have you checked in net tab in the browser dev console if the file is posted? Commented Dec 12, 2022 at 17:12
  • 1
    var_dump($_FILES); Commented Dec 12, 2022 at 17:29

1 Answer 1

1

Files are stored in $_FILES array. $_FILES is array of items uploaded to the current script via the HTTP POST method. see https://www.php.net/manual/en/reserved.variables.files.php

var_dump($_FILES)

and make sure that file_uploads is set to On in the php.ini file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.