A general Restful API in PHP woks along with MySQL database as a management system to facilitate uploading, downloading and browsing of various game related assets.
-
- Apache - php - phpMyadmin - mysql
-
- Linux
scp โr Desktop/UnityAPI/ [email protected]:~/public_html
Local Repository:Desktop/UnityAPI/
//change this to your folder
Server Repository:[email protected]:~/public_html
//change this to your own
- Windows
I'm not suggesting using windows bro.
But if you insist, just copy and paste this folder to your target place
- Linux
๐ Detailed comments in imageServer code
//change this to your servername for phpMyadmin
$servername = null;
//change this to your username for phpMyAdmin
$username = "root";
//change this to your password for phpMyAdmin
$password = null;
//change this to your database name
$dbName = "Server";
//change 'Table' and 'Name' according to your database
$sql = "select * from Table where Name='" . $FileName . "'";
//change 'Table' and 'Name' according to your database
$sql = "INSERT INTO Table (Name) VALUES ('" . $FileName . "')";
//change this to your serverName for phpMyadmin
$servername = null;
//change this to your username for phpMyAdmin
$username = "root";
//change this to your password for phpMyAdmin
$password = null;
//change this to your database name
$dbName = "Server";
//change 'Table' according to your database
$sql="SELECT * FROM 'Table'";
//change 'Name' according to your database
echo "FileName:".$row ['Name'].";";
//The only thing you need to do is changing ".png" to the file type you want to upload
$myFile = $file_path.$_REQUEST['Name'].".png";
string GetDataValue(string data, string index)
{
Debug.Log("data: " + data + "index" + index);
string value = data.Substring(data.IndexOf(index) + index.Length);
if (value.Contains("|"))
{
value = value.Remove(value.IndexOf("|"));
}
return value;
}
WWWForm musicForm = new WWWForm();
//change this to your url
WWW wwwMusic = new WWW("Your itemsData.php url");
yield return wwwMusic;
string allString = (wwwMusic.text);
Debug.Log(allString);
//seperate each tuples
items = allString.Split(';');
string filename;
for (int i = 0; i < items.Length - 1; i++)
{
filename = GetDataValue(items[i], "FileName:");
}
After these part , you would get an array of the filenames, find a specific way for the files you need to load
public static string insertIntoDB(string filename, byte[] bytes)
{
string result="";
WWWForm form = new WWWForm();
form.AddField("fileNamePost", filename);
//change this to your url
string insertURL = "Your insertData.php url";
WWW www = new WWW(insertURL, form);
if (www.error != null)
{
Debug.Log(www.error);
}
else
{
while (!www.isDone)
{
}
Debug.Log("insertNew wav file: " + www.text);
if (www.text != "exist")
{
result= "success";
upload(bytes, filename);
}
else
{
result="fail";
}
}
return result;
}
public static void upload(byte[] bytes, string filename)
{
//change this to your url
string url = "Your UnityUpload.php url";
WWWForm form = new WWWForm();
form.AddField("Name", filename);
form.AddBinaryData("post", bytes);
WWW www = new WWW(url, form);
}