Hi.
I have a GUIButton that should take a screenshot and then upload it to server. This is the code:
void takePhoto(){
console = "Taking photo.";
if (settings.debug == true) {
Debug.Log ("Taking photo.");
}
StartCoroutine(uploadPhoto());
}
IEnumerator uploadPhoto(){
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
tex.ReadPixels( new Rect(0, 0, width, height), 0, 0 );
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
Destroy( tex );
WWWForm form = new WWWForm();
form.AddField("key", "value");
form.AddBinaryData("file", bytes, "screenshot.png", "image/png");
WWW w = new WWW(url, form);
yield return w;
if (!string.IsNullOrEmpty(w.error)) {
Debug.Log(w.error);
}
else {
Debug.Log("Finished uploading.");
}
}
And this is php script to save this screenshot on server:
if ($_POST){
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error code " . $_FILES['uploadedfile']['error']);
} else {
if ((($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000000000)){
if ($_FILES["file"]["error"] > 0) {
echo "File error: " . $_FILES["file"]["error"] . "";
}else{
echo "Uploaded image: " . $_FILES["file"]["name"] . "";
echo "Type: " . $_FILES["file"]["type"] . "";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb";
echo "Temporary name: " . $_FILES["file"]["tmp_name"] . "";
if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
} else{
echo "Invalid file";
}
}
print_r($GLOBALS);
} else{
echo "No POST. ";
}
Now I’ll expain you the problem. It seems that WWW is always returning “500 Internal Server Error” and the screenshot is not uploading.
But if I send an empty form to the server (without BinaryData) the php script is executing. It seems like there is something wrong with the client-side code, but I can’t find a solution.
Can you tell me what am I doing wrong?