I'm doing a webpage where the user needs to load a photo. Since this photo will be linked to a unique id, I want that foto to be transfered to the server and saved as NUMBER.jpg or NUMBER.png or whatever extension. So the event on an input type field is called, in javascritpt code, as follows:
var file = document.getElementById("foto").files[0];
document.getElementById("pathFoto").value = file.name;
var formData = new FormData();
formData.append('foto',file,file.name);
var xhr = new XMLHttpRequest();
xhr.open('POST','php/handler.php',true);
xhr.onload = function() {
if (xhr.status == 200){
console.log("Done");
}
else{
console.log("An error ocurred " + xhr.responseText);
}
}
xhr.send(formData);
On the php side of things, this is what I do:
$image = $_POST["foto"];
file_put_contents("atest.jpg",$image);
Now the error I get is that "foto" is an undefined index. Can anyone tell me what I'm doing wrong?