I’ve been trying to make it so I can upload files from within my game but no matter what I try I seem to get the error "400 bad request.
Unity C#:
IEnumerator UploadToServer (string fileName, string uploadURL) {
WWW localFile = new WWW("file:///" + fileName);
WWWForm form = new WWWForm();
form.AddBinaryData("file", localFile.bytes, "Testing", "text/plain");
WWW upload = new WWW(uploadURL, form);
yield return upload;
if(upload.error == null){
print("Upload successful " + upload.text);
} else {
print("Failed " + upload.error);
}
}
PHP:
<?php
if ($_FILES["file"]["error"] > 0){
echo "Error: " . $_FILES["file"]["error"];
} else {
echo "Upload: " . $_FILES["file"]["name"];
echo "Type: " . $_FILES["file"]["type"];
echo "Size: " . ($_FILES["file"]["size"] / 1024) . "kB";
if(file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . "already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "images/" . $_FILES["file"]["name"]);
}
echo "Stored in: " . $_FILES["file"]["name"];
}
?>
This happens with whatever file I try to upload any kind of file.