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.

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.