I am currently trying to upload a screenshot taken from Unity and Post it to my web server, storing it in a folder. (I am using XAMPP for testing this)
I can take the screenshot without any problems, but I think I'm doing something wrong when trying to send it as a Post in my script.
I've tried to make a basic html form and test it and in this way I can store it successfully, but when trying from my Unity project. Nothing happens.
I have a folder named "upload" and it is located in the same folder as the php script.
I have searched for an answer, but had not found anything that has been working so far. And since every other similar question is about the Legacy WWW function in Unity. I've decided to try and make a new thread.
Hope this makes some kind of sense, and any help would be appreciated.
Here is my code from Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
public class ScreenShotScript : MonoBehaviour
{
bool grab = false;
byte[] bytes;
private void Update()
{
//Press space to start the screen grab
if (Input.GetKeyDown(KeyCode.Space)) {
grab = true;
TakePicture();
}
}
//Function to take a screenshot.
public void TakePicture()
{
if (grab)
{
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
bytes = tex.EncodeToPNG();
Destroy(tex);
//Testing to see if I can store the image local
File.WriteAllBytes(Application.dataPath + "/screenShot.png", bytes);
Debug.Log("Saved the Image!");
StartCoroutine(SendImage());
}
}
public IEnumerator SendImage()
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormFileSection("file", bytes));
UnityWebRequest www = UnityWebRequest.Post("http://localhost/postimage-test.php", formData);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
Php Script:
<!--For testing purpose, to see if I can upload an image by a html form. -->
<html>
<body>
<form action="postimage-test.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="file">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
<?php
if ($_POST) {
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
//Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
}
//Else everything is ok, try to upload file
else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
// }
}
else {
echo "No Post! ";
}
?>