0

I'm building a little web-app where I'd like the users to be able to upload files, I'm using FormData to do so, /

<input id="fileInput" type="file">

var formData = new FormData()
var request = new XMLHttpRequest();
request.onreadystatechange = function () {window.resp = this}
request.open("POST", "upload.php");
request.send(formData);

But I have no idea how to get the data using PHP. I know you can normally do

$_POST["KEY"]

But in this case I'm not using a key because the data isn't a string.

I've searched for quite some time now and came across the following things

print_r($_POST) // returned an empty array
var_dump($_POST) // returned an empty array

I don't know what I'm doing wrong and it's probably just some thing you have to know, but I can't seem to figure it out. Thanks in advance.

EDIT Turns out you can just get the file using the

$_FILES

global, huge thanks to @tobias K!

SOURCES: https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

Florisweb
  • 38
  • 1
  • 8
  • Did you check what is being sent in the network tab of the browsers DevTools? Also, try `$_FILES`. – Tobias K. Jul 08 '18 at 10:59
  • [POST method uploads](http://php.net/manual/en/features.file-upload.post-method.php) – simon Jul 08 '18 at 11:01
  • Possible duplicate of [How to post JSON to PHP with curl](https://stackoverflow.com/questions/813487/how-to-post-json-to-php-with-curl) – Ulrich Dohou Jul 09 '18 at 04:52

1 Answers1

2

If you are posting raw data, you can get it in PHP by

$rawData = file_get_contents("php://input");
Jacob
  • 1,776
  • 14
  • 11