Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bentley100 · Feb 08 at 08:20 AM · texture2dimageserveruploaddownloadable content

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bentley100 · Feb 09 at 07:57 AM 0
Share

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

avatar image Bunny83 Bentley100 · Feb 09 at 12:02 PM 0
Share

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.

avatar image Bentley100 Bunny83 · Feb 11 at 08:19 AM 0
Share

i have already deleted the functions. thanks and can you please link the php.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help uploading an image. 1 Answer

How to grab POST image data from WWWForm on server? 1 Answer

Create, export, and then load a huge image. 2 Answers

Continuously scanning and recognising text with Unity3D on Android 0 Answers

How to save Texture2D to BMP? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges