I’ve been piecing a few things together but I’ve hit a bit of a snag. This is my first attempt at saving an image to a server. I’m including the components I have worked up so far. cs script, php file and my table setup. Right now I’m saving to server but I’m getting empty entries. Can someone show me how to set things up properly in my code?
How do I transfer ‘bytes’ (byte array in C# file) to the server and save it in the ‘image’ blob? How do I pass the form.AddBinaryData paramaters and store them in the table that I’ve created? Based on the code I have written above, can someone tell me why this isn’t writing the screenshot to the MySQL DB? Could someone point out my errors?
---------- C# ----------
public string screenShotCGIURL= "https://locomoku.com/projects/samnoble/animalmatch/php/upload.php";
int yourInt;
public void TakeScreenshot()
{
StartCoroutine("UploadPNG");
}
IEnumerator UploadPNG()
{
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height/2;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
// Create a Web Form
WWWForm form = new WWWForm();
// Set file name
string name = "animalmatch_" + System.DateTime.Now.ToString("MM-dd-yyyy_HH-mm-ss");
name = name+".png";
Debug.Log("The image name is " + name);
// Calculate image size
int size = tex.GetRawTextureData().Length;
Debug.Log("The image size is " + size);
// Upload to a cgi script
WWW upload = new WWW(screenShotCGIURL, form);
form.AddField("frameCount", Time.frameCount.ToString());
form.AddField("image_id", "");
form.AddField("image_type", "image/png");
form.AddBinaryData("image", bytes, name, "image/png");
form.AddField("image_size", size);
form.AddField("image_name", name);
yield return upload;
Debug.Log(upload);
if (!string.IsNullOrEmpty(upload.error)) {
print(upload.error);
}
else {
print("Finished Uploading Screenshot");
}
}
---------- PHP ----------
$db_host = "localhost"; // MySQL Host Name.
$db_user = "********"; // MySQL User Name.
$db_password = "********"; // MySQL Password.
$db_name = "********"; // MySQL Database Name.
$imgData = file_get_contents($filename);
$size = getimagesize($filename);
mysql_connect("$db_host", "$db_user", "$db_password");
mysql_select_db ("$db_name");
$sql = "INSERT INTO screenshot
(image_id ,image_type ,image, image_size, image_name)
VALUES
('', '{$size['mime']}', '{$imgData}', '{$size[3]}',
'{$_FILES['userfile']['name']}')";
mysql_query($sql);
----------mySQL ----------
image_id tinyint(6) not null default '0',
image_type varchar(25) not null default '',
image largeblob not null,
image_size varchar(25) not null default '',
image_name varchar(50) not null default ''