如何在php服务器中创建custome文件夹,同时将文件上传到服务器



我使用以下代码在PHP的帮助下将图像从unity上传到服务器。但它只上传到名为"的单个文件夹;uploaded_images";。我想根据登录id创建自定义文件夹。如何做到这一点。如何将自定义文件夹名称发送到PHP。我不太擅长编程,所以我从Github复制了这段代码。任何人,请帮忙。提前感谢

<?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]";
}
?>

这些是使用的c#脚本

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> OnErrorAction;
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 OnError (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)
OnErrorAction (w.error); //or OnErrorAction.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;
}
}

如果您的上传使用uploaded_files文件夹,那么您可以像下面这样更改服务器代码。

<?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]";
}
?>

您可以从Unity3d客户端文件发送userId。我希望它能为你工作。

相关内容

  • 没有找到相关文章

最新更新