- Home /
Screenshot script not uploading PNG to server when used with key trigger
Hello. I have tried using the script provided in the Unity documentation for uploading the screenshot to the server. It seems to work and upload the PNG to the server. I modified the script to run on a "Jump" button trigger. But it does not seem to work. The "Jump" button is not used by any other script. Here is the script #pragma strict
function UploadPNG() {
// We should only read the screen after all rendering is complete
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24, false );
// Read screen contents into the texture
tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
var bytes = tex.EncodeToPNG();
Destroy( tex );
// Create a Web Form
var form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("file", bytes, "screenShot.png", "image/png");
// Upload to a cgi script
var w = WWW("www.mydomain-s.com/unity/screenshot4/screenshot4.php", form);
yield w;
if (w.error != null){
print(w.error);
Application.ExternalCall( "debug", w.error);
//print(screenShotURL);
}
else{
print("Finished Uploading Screenshot");
//print(screenShotURL);
Application.ExternalCall( "debug", "Finished Uploading Screenshot");
}
}
function Update () {
if(Input.GetButtonUp("Jump")){
UploadPNG();
}
}
And here is the php script
<?php
if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "
"; } else { echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
"; echo "Temp file: " . $_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"; }
?>
The web directory "Upload" has the write permissions set.
I'm stuck at it since hours. Would really appreciate the help.
Have you checked if your UploadPNG script is called? Did you modify anything inside the UploadPNG function or in your PHP script after you successfully tested it?
The UploadPNG() is unchanged. When the function is called from Start() without any trigger, the screenshot gets uploaded. But when a trigger is implemented in the Update() it is not working. I have tested the script with debug, and the console does gives the "Finished Uploading Screenshot" message. But when tested on server, nothing happens.
Strange indeed. Are you able to track if the request reached server at all? You can also take a look at the request in your browser - does it contain texture data? If not, then please check bytes.Length after encoding to PNG.
Answer by anirudha_vfx · Oct 28, 2013 at 05:30 AM
Its solved. You need to add full URL with "http" in the www function.
Thanks ArkaneX. Appreciate your time.