I am making a basic web interface to upload files and my button successfully sends a POST request with a payload that contains the image data, but in the php script the $_FILES array is empty. I've used the chrome dev tools to check my html and javascript but there are no errors. Also I checked the apache error log and there were also no errors, so I am at a bit of a loss as to what is going wrong.
I was following this example and modified it slightly.
Here is the html and javascript
<html>
<head>
Image Uploader
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1> Upload Your Images Here </h1>
<input type="file" id="fileElem" style="display:none" onchange="handleFiles(this.files)">
<button id="fileSelect"> Select some files </button>
<script>
$("#fileSelect").click(function()
{
$("#fileElem").click();
});
function handleFiles(image)
{
console.log("Handling Files");
var reader = new FileReader();
var xhr = new XMLHttpRequest();
xhr.open("POST", "getImage.php");
xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
reader.onload = function(evt)
{
xhr.send(evt.target.result);
console.log("sent");
};
reader.readAsBinaryString(image[0]);
}
</script>
</body>
</html>
Here is the php script.
<?php
print_r($_FILES);
print_r($_POST);
print(count($_FILES));
echo "Reached End";
?>
This is the response I get back from the script. It just shows the empty arrays.
Array
(
)
Array
(
)
0Reached End
I tried different types of files and got the same empty array each time. I made sure file uploads are enabled in the php.ini and the post and file size limits are high enough (80M). If it helps I am running an apache2 server with php 7.0 installed all on a raspberry pi.
I've also checked the apache error log but there are no errors.