First, I would like to mention that I am developing an electronic tour of our school that will be deployed on many desktop computers. In each client, the admin can access all the administrative tools by logging in. So if the admin for example want to add an information about a building, that information will be uploaded to a web server(I am using apache, mysql, and php) using one of the deployed client and the info will be save to mysql via php. And also one of the requirement of the system is to be able to load a photo or video on local file system of one of the client and then upload it to the web server(installed on one of the computer in the LAN).
So far the system can upload text information. What I'm having trouble with, is the uploading of photos and videos. Any tips, links, or tutorials on how to achieve this will be greatly appreciated.
Btw, I already search on google on how to achieve this, but no luck so far.
Hi Guys, You can use this plugin [Easy Downloader and Uploader][1] [1]: http://u3d.as/9Ee
Hey, Im pretty new to WWW/UnityWebRequest and PHP, and im working on it for school project atm. I have been trying to upload a video to a server through a post request with php, When i do the post request with Postman it works fine. When i do it through unity it returns that the file is empty, (while my byte[] i have is filled) I wonder if you had the same problem or if it is not at all related.
Use the WWW class to open/load the local file or use System.IO stuff for reading.
To upload the file to your webpage use WWWForm along with WWW. There’s even an example of how to upload a screenshot on the WWWForm reference page. I guess you’re familiar with the backend part (php, mysql).
SECOND EDIT:
// C# file names: "FileUpload.cs"
using UnityEngine;
using System.Collections;
public class FileUpload : MonoBehaviour
{
private string m_LocalFileName = "C:/boot.ini";
private string m_URL = "http://192.168.178.29/php/upload.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,"text/plain");
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();
}
}
I have a xitami webserver on a windows machine(actually a very old laptop with Win98 :D) and use this php script:
hmm, well, that sounds like a pure php problem and i'm not that familiar with php ;) just have done some basic things and had to look up a lot. Maybe someone else have more experience with uploading files and php processing. In general is it a problem when the "file data" comes in a post var? I don't know it that really happens, i think the file will still be in $_FILES. Just take a look at this: http://www.tizag.com/phpT/fileupload.php
Any help about why it kept returning "Failed!" and how to fix it will be appreciated.
Hmm, strange, are you sure the local file can be loaded? I've read somewhere that "file://" won't work in all cases and you should use "file:///". Are you sure your webserver/phpconfig allows uploads? If not that would explain the "failed". In my "new" example i've added some more error checking to the coroutine. The error checking on php side could be better but it works for me ;)
Smallest and easy Solution is using UnityWebRequest, as WWW is obsolete. This will work in latest and upcoming Unity Versions
using UnityEngine.Networking;
public IEnumerator PostImageToPhp()
{
var file = UnityWebRequest.Get("file://C:/star.png");
yield return file;
WWWForm form = new WWWForm();
form.AddBinaryData("file", file.downloadHandler.data);
var upload = UnityWebRequest.Post("http://localhost/Sample/GetFile.php", form);
yield return upload.SendWebRequest();
if (upload.isHttpError)
Debug.Log(upload.error);
else
Debug.Log("Uploaded Successfully");
Debug.Log(upload.downloadHandler.text); // display whether Server got the File
}
PHP Code
<?php
$file = $_FILES["file"];
if (!empty($file))
echo "Got file";
else
echo "Failed to get File";
?>
is only that php code neccesary? or Do I need to add something else?
Hi Guys, You can use this plugin [Easy Downloader and Uploader][1] [1]: http://u3d.as/9Ee
– DeveshPandeyHey, Im pretty new to WWW/UnityWebRequest and PHP, and im working on it for school project atm. I have been trying to upload a video to a server through a post request with php, When i do the post request with Postman it works fine. When i do it through unity it returns that the file is empty, (while my byte[] i have is filled) I wonder if you had the same problem or if it is not at all related.
– Teuntje001