从Unity将图像上传到数据库

时间:2019-07-03 09:01:57

标签: c# php unity3d server

如何将图像从Unity上传到服务器上? 经过研究,我遇到了以下代码,并尝试通过下面给出的php将其上传到服务器,但是它不起作用。请提供任何帮助,需要一些指导。

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();
    }
}

}

和下面的php代码:

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

尽管我从Unity收到成功消息,但从服务器中的php收到失败消息。

0 个答案:

没有答案
相关问题