1

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.

https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications#Example_Uploading_a_user-selected_file

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.

  • 1
    `$_FILES` is a PHP commodity to parse attachments in requests that use [multipart/form-data](https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean). If you use a custom encoding you need to write custom decoding. – Álvaro González Dec 10 '18 at 07:34

0 Answers0