0

This is my code, I'm trying to send a file to a PHP file but I can't. In JS the answer is that input_f.files[0] is an [object File], however PHP returns Notice: Undefined index: file... , I think that formData doesn't work.

PHP Code

<?php
$file = $_FILES['file']['size'];
echo $file;

JS Code

var text = document.getElementById('text');
var input_f = document.getElementById('input_f');//Input file

var xmlhttp = new XMLHttpRequest();
var formData = new FormData();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        text.innerHTML = this.response;
    }
};
xmlhttp.open("POST","php/convert.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
formData.append('file', input_f.files[0]);
xmlhttp.send(formData);
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Andres
  • 23
  • 3

1 Answers1

0

Try this

<?php
    $file = $_FILES['file']['size'];
    echo $file;
    JS Code

    var text = document.getElementById('text');
    var input_f = document.getElementById('input_f');//Input file

    var xmlhttp = new XMLHttpRequest();
    var formData = new FormData();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            text.innerHTML = this.response;
        }
    };
    xmlhttp.open("POST","php/convert.php");
    formData.append('file', input_f.files[0]);
    xmlhttp.send(formData);
Kaustubh Kadam
  • 182
  • 3
  • 13