- Home /
WWWForm always returning Internal Server Error
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?
Answer by Dave-Carlile · Jul 15, 2016 at 08:21 PM
An Internal Server Error would indicate that there's something wrong on the server side, not the client. Your PHP script isn't understanding something that's being sent to it. An easy way to debug these things is to actually run the PHP code from a web browser. Set up a real web form to upload the picture and debug the PHP that way. Once you have it working standalone, then integrate it with your Unity code.
I did it. It works standalone as intended, but when I integrate it with Unity it gives me Internal Server Error again.
Is my C# code correct? Because I rewrote the code several times and it still doesn't work.
I'm getting the same error. It has to be the server not being able to understand the binary data. I'm not sure what needs to change with that. I remade my php page to just echo back a constant and it still produces a 500 error. In my case it is specifically the binary. Still searching for a solution. This bug was not happening in the same code a year ago. If I find a solution I'll post it.
Well, the solution was simple. I wrote about the problem to my hosting, they replied that they needed to disable some anti-spam filters. I suggest you doing the same :) Let me know if it works!
$$anonymous$$y web host claims that they are doing nothing wrong and the problem is with my code. No signs of security issues, and any problems with headers are my problems. If I change the url to point to a php script on another website and there error doesn't happen.
Your answer
Follow this Question
Related Questions
PHP POST through $_REQUEST with WEBBUILD 1 Answer
Send an integer array through WWWForm 1 Answer
WWW form isn't working in Build 1 Answer
Is there a maximimun number of fields for WWWForm.AddField? 1 Answer
Multiple Cars not working 1 Answer