- Home /
Upload an image onto the database from Unity
How do I upload an image onto the server from Unity? After researching I came across the below code and tried to upload it to the server through the php given below but it does not work. Any help please, need some guidance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UploadImage : MonoBehaviour
{
private string m_LocalFileName = "E:/house.png";
private string m_URL = "http://localhost/image.php";
IEnumerator UploadFileCo(string localFileName, string uploadURL)
{
WWW localFile = new WWW("file:///" + localFileName);
yield return localFile;
if (localFile.error == null)
Debug.Log("Loaded file successfully");
else
{
Debug.Log("Open file error: " + localFile.error);
yield break; // stop the coroutine here
}
WWWForm postForm = new WWWForm();
// version 1
//postForm.AddBinaryData("theFile",localFile.bytes);
// version 2
postForm.AddBinaryData("theFile", localFile.bytes, localFileName, "image/png");
WWW upload = new WWW(uploadURL, postForm);
yield return upload;
if (upload.error == null)
Debug.Log("upload done :" + upload.text);
else
Debug.Log("Error during upload: " + upload.error);
}
void UploadFile(string localFileName, string uploadURL)
{
StartCoroutine(UploadFileCo(localFileName, uploadURL));
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
m_LocalFileName = GUILayout.TextField(m_LocalFileName);
m_URL = GUILayout.TextField(m_URL);
if (GUILayout.Button("Upload"))
{
UploadFile(m_LocalFileName, m_URL);
}
GUILayout.EndArea();
}
}
}
and the php code
<?php
Although I get a success message from Unity but I get a failed message from php in the server.
if(isset($_FILES['theFile']))
{
print("Success! ");
print("tmpName: " . $_FILES['theFile']['tmp_name'] . " ");
print("size: " . $_FILES['theFile']['size'] . " ");
print("mime: " . $_FILES['theFile']['type'] . " ");
print("name: " . $_FILES['theFile']['name'] . " ");
move_uploaded_file($_FILES['theFile']['tmp_name'], "../image/" . $_FILES['theFile']['name']);
}
else
{
print("Failed!");
}
?>
Although I get a success message from Unity but I get a failed message from php in the server.
Could you please post the Error message you get from PHP ? :)
$$anonymous$$ight be that it's pointing out what's wrong / not working.
Also - A small side comment. Please keep in $$anonymous$$d that users might potentially use this to abuse the upload form and potentially spam your sever with thousands of uploads / files, so unless this is for an App of sorts or people you totally trust - keep it in $$anonymous$$d. As some might simply "sniff" out the logic behind this upload and start calling it outside of your Unity build to endlessly keep adding new files or call your PHP script.
Answer by Bunny83 · Jul 03, 2019 at 09:31 PM
If you get back the string "Failed!" from your server, this is most likely an issue on your server. Have a look at this post. When you use AddBinaryData on a WWWForm Unity always creates a post with "multipart/form-data". There are countless of things that could be misconfigured on your PHP side. So make sure you actually allow file uploads in your PHP.ini (file_uploads = On). So check your settings and try
var_dump($_FILES);
in your php script.
Your answer
Follow this Question
Related Questions
How to get Variable from Php Script? 0 Answers
How to make a highscore database for unity? 1 Answer
WebHosting with phpMyAdmin PHP 0 Answers
Multiple Cars not working 1 Answer
php, sql security 3 Answers