I'm using Unity to make a button that takes a screenshot and then uploads it to my server.
The point is that if I send a form to the server, the WWW gives me an Internal Server Error.
I've tried a lot of examples from the internet and it still gives me that error. Every time.
This is my C# code for uploading the screenshot:
IEnumerator uploadPhoto(){
/*yield return new WaitForEndOfFrame();
Texture2D snap = new Texture2D (webCamTexture.width, webCamTexture.height);
snap.SetPixels (webCamTexture.GetPixels ());
snap.Apply ();
webCamTexture.Stop();*/
yield return new WaitForEndOfFrame();
Texture2D snap = new Texture2D(Screen.width, Screen.height);
snap.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
snap.Apply();
byte[] bytes = snap.EncodeToPNG();
WWWForm form = new WWWForm();
form.AddField("username", PlayerPrefs.GetString("username"));
form.AddBinaryData("form_file", bytes, "screenshot.png", "image/png");
//Debug.Log(System.BitConverter.ToString(bytes));
WWW connection = new WWW(url, form);
yield return connection;
if (connection.error != null)
{
Debug.Log("Server-side error: " + w.error);
}
else
{
Debug.Log(connection.text);
}
}
And this is the php script made to receive that BinaryData:
if ($_POST){
if ($_FILES["form_file"]["error"] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES["form_file"]["error"]);
} else {
if ((($_FILES["form_file"]["type"] == "image/jpg") || ($_FILES["form_file"]["type"] == "image/jpeg") || ($_FILES["form_file"]["type"] == "image/png")) && ($_FILES["form_file"]["size"] < 20000000000)){
if ($_FILES["form_file"]["error"] > 0) {
echo "File error: " . $_FILES["form_file"]["error"] . "";
}else{
echo "Uploaded image: " . $_FILES["form_file"]["name"] . "<br>";
echo "Type: " . $_FILES["form_file"]["type"] . "<br>";
echo "Size: " . ($_FILES["form_file"]["size"] / 1024) . " Kb<br>";
echo "Temporary name: " . $_FILES["form_file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["form_file"]["name"])){
echo $_FILES["form_file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["form_file"]["tmp_name"], "upload/" . $_FILES["form_file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["form_file"]["name"];
}
}
} else{
echo "Invalid file";
}
}
} else{
echo "No POST. ";
}
I don't know what's wrong with this. I'm trying to make this work for 2 days now.
I just know that the error is on the server-side. I tried to change the permissions of the script to 777, no effect.
What am I doing wrong?