Search code examples
c#phpunity-game-engineinternal-server-error

WWW POST gives me Internal Server Error


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?


Solution

  • Copied your php code to my server and came to conclusion that the problem is from your server. It is not setup properly or this is permission problem which also means that it is not set up properly.

    With the php in your question, I got the message from my test server:

    Uploaded image: screenshot.png<br>Type: image/png<br>Size: 1.494140625 Kb<br>Temporary name: C:\Windows\Temp\php1354.tmp<br>Stored in: upload/screenshot.png
    

    When ran on your server, I got:

    Server-side error: 500 Internal Server Error
    

    The php code I used:

    <?php
    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. ";
    }
    ?>
    

    When I used form.AddField("username", PlayerPrefs.GetString("username")); on your server, I got Upload failed with error code. After including form.AddBinaryData("form_file", bytes, "screenshot.png", "image/png");, I got the 500 error message.

    This is a big sign that you don't have permission to read write the file. Also the image was written to C:\Windows\Temp\php1354 on my server. You have to change your php code to write to somewhere else instead of the C:\Windows\Temp\php1354 directory. I would suggest you contact your host to change the permission. This is a server problem. Not Unity or php code fault.