- Home /
Get multiple images from online server and download them to android app
Hi, i a working on a sort of app where you can text and upload images to a main page. i have a problem where i can upload an multiple images to [my server][1] and then retrieve all of them and display the images on a scroll or something like that. i also don't want the app to like instantly freeze when it tries t download them. Here is my scripts,
image downloader
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class imagefinder : MonoBehaviour
{
public Image imageToDisplay;
string url = "---stripped for security reasons---";
void Start()
{
StartCoroutine(loadSpriteImageFromUrl(url));
}
IEnumerator loadSpriteImageFromUrl(string URL)
{
WWW www = new WWW(URL);
while (!www.isDone)
{
Debug.Log("Download image on progress" + www.progress);
yield return null;
}
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log("Download failed");
}
else
{
Debug.Log("Download succes");
Texture2D texture = new Texture2D(1, 1);
www.LoadImageIntoTexture(texture);
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height), Vector2.zero);
imageToDisplay.sprite = sprite;
}
}
}
this is the two controller scripts
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
public enum ImageType
{
PNG,
JPG
}
public class ImageUploader : MonoBehaviour
{
Texture2D imageTexture;
string fieldName;
string fileName = "myimage";
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;
}
}
and this is the second one
using UnityEngine;
public class Test : MonoBehaviour
{
[SerializeField] SpriteRenderer imageSprite;
[SerializeField] string serverUrl;
public void StartUploade ()
{
ImageUploader
.Initialize ()
.SetUrl (serverUrl)
.SetTexture (imageSprite.sprite.texture)
.SetFieldName ("myimage")
.SetFileName ("myimage")
.SetType (ImageType.JPG)
.OnError (error => Debug.Log (error))
.OnComplete (text => Debug.Log (text))
.Upload ();
ImageUploader
.Initialize ()
.SetUrl (serverUrl)
.SetFieldName ("myimage")
.Upload ();
}
}
thx
Answer by Bunny83 · Feb 08 at 12:53 PM
move_uploaded_file($tmpimg, "./uploaded_images/$img");
Are you crazy -.-
Do you have the slightest idea what you've done here? What if I send you a php file with the name
"someCode.php"
Your server would happily store that file on your server. Now I can simply access this file and run it on your server. Likewise I can even use a name like "../../someCode.php"
and it would happily store the file in your root folder.
I would strongly recommend to take down your php file ASAP and work on your data validation on the server side.
Apart from that I'm wondering what you're asking specifically. Your php file just generates html to simply list all images as an html page. You don't provide any API that allows to query which files are present on your server, preferably in a machine readable format like json or csv.
Also currently your server has directory listing enabled. You should setup an .htaccess file (or whatever applies to your server) to restrict access to the server internals.
i dont really $$anonymous$$d about the server there is no files on it. i could just delete it and make another one easily. i have only been doing this for 3 days and i have no idea about this stuff. and thanks for the reply will do a .htaccess file thanks
I wrote you a PM (in the unity forum) about your security issues. I also uploaded a new php file called "getImageListFixed.php" which returns the found images as a json object. You need an API like that so the client knows which images exist on the server.
ps: when I said you should remove your php file I meant remove it from the server, not from your question ^^. Currently someone could abuse your server for literally anything. You are responsible and liable for the server you rented. Someone may store stolen credit card informations on your server, use it in a DDOS attack or whatever. At least, deactivate your upload function for the time being.
i have already deleted the functions. thanks and can you please link the php.
Your answer
Follow this Question
Related Questions
Need help uploading an image. 1 Answer
How to grab POST image data from WWWForm on server? 1 Answer
How do you crop a Texture2d 1 Answer
Read/Write file to a server. Android 1 Answer
Using of Texture2D.LoadImage 0 Answers