Hello to everyone
I can’t send my png file to my website. everything looks right.
I couldn’t solve the problem. Can you help with this?
c # code I use:
IEnumerator Start()
{
//You can load the image as a byte array
yield return UploadPNG();
}
IEnumerator UploadPNG()
{
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// For testing purposes, also write to a file in the project folder
// File.WriteAllBytes(Application.dataPath + "/../SavedScreen.png", bytes);
byte[] byteArray = File.ReadAllBytes(Application.dataPath + "/resim.png");
// Create a Web Form
WWWForm form = new WWWForm();
form.AddField("resim", Time.frameCount.ToString());
//form.AddBinaryData("resim", Camera_Play.bytes);
form.AddBinaryData("file", byteArray, "resim.png", "image/png");
// Upload to a cgi script
var w = UnityWebRequest.Post("http://digigia.com/panel/upload.php", form);
yield return w.SendWebRequest();
if (w.isNetworkError || w.isHttpError)
{
Debug.Log(w.error);
}
else
{
Debug.Log("Finished Uploading Screenshot");
}
Debug.Log(w.downloadHandler.text);
}
this is the web php:
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {
$currentDir = getcwd();
$uploadDirectory = "/uploads/";
$errors = [ ]; // Store all foreseen and unforseen errors here
$fileExtensions = ['jpeg','jpg','png']; // Get all the file extensions
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
$fileTmpName = $_FILES['myfile']['tmp_name'];
$fileType = $_FILES['myfile']['type'];
$fileExtension = strtolower(end(explode('.',$fileName)));
$uploadPath = $currentDir . $uploadDirectory . basename($fileName);
if (isset($_POST['resim'])) {
if (! in_array($fileExtension,$fileExtensions)) {
$errors[ ] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 2000000) {
$errors[ ] = "This file is more than 2MB. Sorry, it has to be less than or equal to 2MB";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been uploaded";
} else {
echo "An error occurred somewhere. Try again or contact the admin";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
}
} else {
//File type was invalid, so throw up a red flag!
echo "Invalid File Type";
}
unity debug screen:
Finished Uploading Screenshot
Invalid File Type