I use the below codes to upload images to a server from unity with help of PHP. but it only uploads to a single folder named "uploaded_images". I want to create custom folders based on login id. How to do that. how to send custom folder names to PHP. I am not that good at programming, so I copied this code from Github. Anyone, please help. thanks in advance
<?php
if (isset($_FILES['myimage'])){
$img = $_FILES['myimage']['name'];
$tmpimg = $_FILES['myimage']['tmp_name'];
//To get file extension
//$fileExt = pathinfo($img, PATHINFO_EXTENSION) ;
move_uploaded_file($tmpimg, "./uploaded_images/$img");
echo "[success] image ($img) uploaded successfully.";
exit();
}
else{
echo "[error] there is no data with name [myimage]";
}
?>
these are the c# scripts used
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
[SerializeField] string serverUrl;
[SerializeField] Image ImageBox;
public string name_of_Image;
public void upload()
{
ImageUploader
.Initialize ()
.SetUrl (serverUrl)
.SetTexture (ImageBox.sprite.texture)
.SetFieldName ("myimage")
.SetFileName (name_of_Image)
.SetType (ImageType.JPG)
.OnError (error => Debug.Log (error))
.OnComplete (text => Debug.Log (text))
.Upload ();
}
}
public class ImageUploader : MonoBehaviour
{
Texture2D imageTexture;
string fieldName;
string fileName = "defaultImageName";
ImageType imageType = ImageType.PNG;
string url;
//Events
UnityAction<string> one rrorAction;
UnityAction<string> OnCompleteAction;
public static ImageUploader Initialize ()
{
return new GameObject ("ImageUploader").AddComponent <ImageUploader> ();
}
public ImageUploader SetUrl (string serverUrl)
{
this.url = serverUrl;
return this;
}
public ImageUploader SetTexture (Texture2D texture)
{
this.imageTexture = texture;
return this;
}
public ImageUploader SetFileName (string filename)
{
this.fileName = filename;
return this;
}
public ImageUploader SetFieldName (string fieldName)
{
this.fieldName = fieldName;
return this;
}
public ImageUploader SetType (ImageType type)
{
this.imageType = type;
return this;
}
//events
public ImageUploader one rror (UnityAction<string> action)
{
this.OnErrorAction = action;
return this;
}
public ImageUploader OnComplete (UnityAction<string> action)
{
this.OnCompleteAction = action;
return this;
}
public void Upload ()
{
//check/validate fields
if (url == null)
Debug.LogError ("Url is not assigned, use SetUrl( url ) to set it. ");
//...other checks...
//...
StopAllCoroutines ();
StartCoroutine (StartUploading ());
}
IEnumerator StartUploading ()
{
WWWForm form = new WWWForm ();
byte[] textureBytes = null;
//Get a copy of the texture, because we can't access original texure data directly.
Texture2D imageTexture_copy = GetTextureCopy (imageTexture);
switch (imageType) {
case ImageType.PNG:
textureBytes = imageTexture_copy.EncodeToPNG ();
break;
case ImageType.JPG:
textureBytes = imageTexture_copy.EncodeToJPG ();
break;
}
//image file extension
string extension = imageType.ToString ().ToLower ();
form.AddBinaryData (fieldName, textureBytes, fileName "." extension, "image/" extension);
WWW w = new WWW (url, form);
yield return w;
if (w.error != null) {
//error :
if (OnErrorAction != null)
one rrorAction (w.error); //or one rrorAction.Invoke (w.error);
} else {
//success
if (OnCompleteAction != null)
OnCompleteAction (w.text); //or OnCompleteAction.Invoke (w.error);
}
w.Dispose ();
Destroy (this.gameObject);
}
Texture2D GetTextureCopy (Texture2D source)
{
//Create a RenderTexture
RenderTexture rt = RenderTexture.GetTemporary (
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear
);
//Copy source texture to the new render (RenderTexture)
Graphics.Blit (source, rt);
//Store the active RenderTexture & activate new created one (rt)
RenderTexture previous = RenderTexture.active;
RenderTexture.active = rt;
//Create new Texture2D and fill its pixels from rt and apply changes.
Texture2D readableTexture = new Texture2D (source.width, source.height);
readableTexture.ReadPixels (new Rect (0, 0, rt.width, rt.height), 0, 0);
readableTexture.Apply ();
//activate the (previous) RenderTexture and release texture created with (GetTemporary( ) ..)
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary (rt);
return readableTexture;
}
}
CodePudding user response:
If your upload is working with uploaded_files folder then you can change your server code like below.
<?php
if (isset($_FILES['myimage'])){
$userId = $_GET['userId']//or $_POST
$img = $_FILES['myimage']['name'];
$tmpimg = $_FILES['myimage']['tmp_name'];
//To get file extension
//$fileExt = pathinfo($img, PATHINFO_EXTENSION) ;
if (!file_exists('./uploaded_images/$userId')) {
mkdir('./uploaded_images/$userId', 0777, true);
}
move_uploaded_file($tmpimg, "./uploaded_images/$userId/$img");
echo "[success] image ($img) uploaded successfully.";
exit();
}
else{
echo "[error] there is no data with name [myimage]";
}
?>
And you can send userId from Unity3d client file. I hope it can work for you.