Well I have this working as a normal script attached to an empty game object and using "Send Message" to initiate the process and assign the name of the file which it is getting from the persistent folder. Here is the code;
using UnityEngine;
using System.Collections;
public class SendFile : MonoBehaviour {
//public string m_LocalFileName = "Testdb.db";
public string m_URL = "http://localhost/test/upload_file.php";
string filename;
/*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"))
{
filename = Application.persistentDataPath + "/" + m_LocalFileName;
Debug.Log(filename);
UploadFile(filename,m_URL);
}
GUILayout.EndArea();
}*/
void UploadStart(string name)
{
filename = Application.persistentDataPath + "/" + name;
Debug.Log(filename);
UploadFile(filename,m_URL);
}
void UploadFile(string localFileName, string uploadURL)
{
StartCoroutine(UploadFileCo(localFileName, uploadURL));
}
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);
}
}
I left the commented out code from the existing script I uploaded earlier for reference. The PHP code is relatively unchanged.
So this will post a database stored in the persistent folder by SQLiteKit to a web server. I am half way there. Now I need to pull a database from the web server and put it into the persistent folder so I can use it with SQLiteKit. Here is where I am at thus far which isn't working as of yet.
using UnityEngine;
using System.Collections;
public class ReceiveFile : MonoBehaviour {
public string url = "http://localhost/test/download_file.php?";
void DownloadStart(string name)
{
WWW www = new WWW(url + name);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
and I am not sure where to start on the PHP end.
So now I have two things to accomplish. First, GET a file from the web server and store it in either the persistent folder or the streaming asset folder. I would prefer the former. Second, is to get these scripts as Actions to be used in playMaker. I think this would be very beneficial to the community.
Any assistance would be greatly appreciated.
~V~