Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Upload File - 400 bad request

Discussion in 'Editor & General Support' started by Ghus86, Feb 17, 2016.

  1. Ghus86

    Ghus86

    Joined:
    Oct 24, 2013
    Posts:
    15
    Hi
    I am trying to upload a screen shot to a server, when i do it in the localhost everthing goes fine but when I put it in a public server I get a 400 bad request.

    Any hint how to solve this issue is appreciates, I have changed permissons, created folders, added the cross domain xml in the root and tested all the scripts I was able to find =/

    Code (CSharp):
    1.     public string screenShotURL= "http://www.mysite.pt/screenshot.php";
    2.    
    3.     // Use this for initialization
    4.     void Start () {
    5.         StartCoroutine(UploadPNG());
    6.     }
    7.    
    8.     IEnumerator UploadPNG() {
    9.         // We should only read the screen after all rendering is complete
    10.         yield return new WaitForEndOfFrame();
    11.        
    12.         // Create a texture the size of the screen, RGB24 format
    13.         int width = Screen.width;
    14.         int height = Screen.height;
    15.         var tex = new Texture2D( 256, 256, TextureFormat.RGB24, false );
    16.        
    17.         // Read screen contents into the texture
    18.         tex.ReadPixels( new Rect(0, 0, 256, 256), 0, 0 );
    19.         tex.Apply();
    20.        
    21.         // Encode texture into PNG
    22.         byte[] bytes = tex.EncodeToPNG();
    23.         Destroy( tex );
    24.        
    25.         // Create a Web Form
    26.         WWWForm form = new WWWForm();
    27.         form.AddBinaryData("file", bytes, "screenShot.png", "image/png");
    28.        
    29.         // Upload to a cgi script
    30.         WWW w = new WWW(screenShotURL, form);
    31.         yield return w;
    32.         if (!string.IsNullOrEmpty(w.error)) {
    33.             print(w.error);
    34.         }
    35.         else {
    36.             print("Finished Uploading Screenshot");
    37.         }
    38.     }
    server code

    Code (CSharp):
    1.  <?php
    2.     if (($_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 20000000000))
    3.     {
    4.         if ($_FILES["file"]["error"] > 0)
    5.         {
    6.             echo "Return Code: " . $_FILES["file"]["error"] . "";
    7.         }
    8.         else
    9.         {
    10.             echo "Upload: " . $_FILES["file"]["name"] . "
    11.            "; echo "Type: " . $_FILES["file"]["type"] . "
    12.            "; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
    13.            "; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
    14.            ";
    15.             if (file_exists("upload/" . $_FILES["file"]["name"]))
    16.             {
    17.                 echo $_FILES["file"]["name"] . " already exists. ";
    18.             }
    19.             else
    20.             {
    21.                 move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    22.                 echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    23.             }
    24.         }
    25.     }
    26.     else
    27.     {
    28.         echo "Invalid file";
    29.     }
    30. ?>