Uploading photo and video on a web server

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.

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:

<?php
   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'], "../images/" . $_FILES['theFile']['name']);
   } else
   {
      print("Failed!");
   }
?>

...and it works great. In Unity i get this as result:

version 1:

upload done :Success! tmpName: C:\WINDOWS\TEMP\php2291.TMP size: 212 mime: application/octet-stream name: theFile.dat

version 2:

upload done :Success! tmpName: C:\WINDOWS\TEMP\php11B2.TMP size: 212 mime: text/plain name: boot.ini

I check it numerous times, but I doesn't make sense to me why it kept returning "Failed!". So I decided to post my test code here.

Here's my C# test code

//C#
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnGUI()
    {
        GUI.Label(new Rect(100, 0, 500, 20), Application.dataPath);
        if (GUI.Button(new Rect(100, 100, 150, 20), "Upload"))
        {
            UploadFile("http://localhost/image.php");
        }
    }

    IEnumerator UploadFileCo(string uploadURL)
    {
        WWW localFile = new WWW("file://G:/0001.png");
        yield return localFile;
        WWWForm postForm = new WWWForm();
        postForm.AddBinaryData("file", localFile.bytes, "0001.png", "image/png");
        WWW upload = new WWW(uploadURL, postForm);
        yield return upload;
        if (upload.error == null)
        {
            Debug.Log(upload.text);
        }
        else
        {
            Debug.Log("Error during upload: " + upload.error);
        }
    }

    void UploadFile(string uploadURL)
    {
        StartCoroutine(UploadFileCo(uploadURL));
    }
}

And here's my php code

<?php

    $thefile = $_FILES['file'];
    if(!empty($theFile))
    {
        echo "Success!";
    }
    else
    {
        echo "Failed!";
    }

?>

Any help about why it kept returning "Failed!" and how to fix it will be appreciated.

php is wrong in above code :
@Sanlaato
$thefile variable case

$thefile = $_FILES['file'];  if(!empty($thefile))  {
     echo "Success!";  }  else  {
     echo "Failed!";  }

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";
?>

@Sanlaato Try this asset Web Request Pro | Network | Unity Asset Store we created this asset to solve this problem and much more!any doubt : https://forum.unity.com/threads/released-basic-web-request-pro-1-0-http-post-and-get-requests-to-a-web-server-php-and-nodejs.761489/