1

I want to upload an image to a server in my application which is done in unity I used this code found in unity forum.When it runs it says it was uploaded successfully but cant find the uploaded file.

I get

upload done :Success! tmpName: C:\xampp\tmp\php2EB.tmp size: 156070 mime: text/plain name: 66.jpg <br />
<b>Warning</b>:  move_uploaded_file(../images/66.jpg): failed to open stream: No such file or directory in <b>C:\xampp\htdocs\DreamHousedb\upload.php</b> on line <b>10</b><br />
<br />

But this file do exist in the localhost

How can I fix this problem ?

FileUpload.cs

using UnityEngine;
using System.Collections;

public class FileUpload : MonoBehaviour
{
    private string m_LocalFileName = "C:/boot.ini";
    private string m_URL = "http://localhost/DreamHousedb/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();
    }


}

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!");
   }
?>

0 Answers0