Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gamemaster · Nov 10, 2015 at 01:15 AM · networkingserverwwwunity4

Uploading a file is not working in Unity

I've been searching for a solution for this for a few days now and haven't found anything (obviously) that works.

I'm trying to upload basic text files to my server from Unity so that players can share their levels. I'm using a php script to handle the file upload and I've tested the script using Postman and it works just fine. But it does not work within Unity via WWW & WWWForm, any (free) alternatives are welcome.

The error I'm getting:

Forbidden

You don't have permission to access /scripts/upload.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

PHP Script

     // Configuration
     $dbhost             = 'localhost';
     $dbname    = 'dbname';
     $user         = "dbuser";
     $pass         = "dbpass";
 
     // Profanity check
     function is_profanity($q,$json=0) 
     {
         $q=urlencode(preg_replace('/[\W+]/',' ',$q));
         $p=file_get_contents('http://www.wdyl.com/profanity?q='.$q);
         if ($json) { return $p; }
         $p=json_decode($p);
         return ($p->response=='true')?1:0;
     }
  
     if ($_POST) 
     {
         if (isset($_POST['author'])) 
         {
             if ($_FILES['file']['error'] === UPLOAD_ERR_OK)
             {
                 if ($_FILES['file']['name'] !== "")
                 {
                     $name         = $_FILES['file']['name'];
                     $author        = $_POST['author'];
                     
                     if (is_profanity($name) == 1)
                     {
                         echo "bad name";
                     }
                     else
                     {
                         try
                         {
                             $db = new PDO("mysql:host=$dbhost;dbname=$dbname",$user,$pass);
                             
                             $uploadfile = '/home/site/public_html/levels/' . $name;
                             move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
                             
                             $insert = $db->prepare("INSERT INTO UserLevels (name, author, location) VALUES (:name, :author, :path)");
                             $insert->bindParam(':name', $name, PDO::PARAM_STR, 255);
                             $insert->bindParam(':author', $author, PDO::PARAM_STR, 255);
                             $insert->bindParam(':path', $uploadfile, PDO::PARAM_STR, 255);
                             $insert->execute();
                                 
                             echo "success";
                         } 
                         catch(PDOException $e) 
                         {
                             echo 'genral error' .$e->getMessage();
                         }
                     }
                 }
                 else
                 {
                     echo "no name";
                 }
             }
             else
             {
                 echo "upload error";
             }
         }
         else
         {
             echo "argument error";
         }
     }
     else
     {
         echo "no result";
     }
 ?>

CS Code public TextAsset testLvl; public string name = "person";

         void UploadMaze()
     {
         byte[] data = testLvl.bytes;
         string fileName = testLvl.name;
 
         WWWForm form = new WWWForm();
                 form.AddField("author", name);
         form.AddField("file", "file");
         form.AddBinaryData("file", data, fileName, "text/xml");
 
         StartCoroutine(Request(new WWW(baseUrl + "upload.php", form)));
     }
 
         IEnumerator Request(WWW www)
     {
         yield return www;
 
         if (www.error == null)
         {
             Debug.Log("Success: " + www.text);
         }
         else
         {
             Debug.Log("Error: " + www.error);
             Debug.Log(www.text);
         }
     }

The Chmod on the script is 644 (default) The folder is 755 (default)

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

2 Replies

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

Answer by Gamemaster · Nov 13, 2015 at 02:09 AM

Looks like I can get around this by simply creating the file with php (using fopen) and then writing the contents (using fwrite) from posting the text.

Comment
Add comment · 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
1

Answer by Bunny83 · Nov 10, 2015 at 01:51 AM

Well, the error "403 Forbidden" as well as "404 Not Found" are server side errors. So it most likely is a problem on your server. It's possible that you use a misformed request of some sort.

For example you add two fields with the same name "file". That's the point of that field anyways:`form.AddField("file", "file");`?

Do you use http or https? What's your target platform and where do you test your code? In the editor or in a build?

Comment
Add comment · Show 2 · 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 Gamemaster · Nov 10, 2015 at 10:34 PM 0
Share

Thanks for your reply.

I derived the two scripts from this post which had the double "file" filed and though I though it was odd it seemed to work for those using it and in Postman. I took it out and that got rid of the 404 error and now the WWW returns a 403 error in the www.error property as opposed to the previous being text.

I'm using http and I'm testing my code in the editor and have only tried testing it in a build once. So far I've only tested on a $$anonymous$$ac but in the end my target platforms will be PC and Wii U for sure with hopes of $$anonymous$$ac and Linux too. The only other thing I can think of to do is change my environment. Am I needing to add some sort of authorization perhaps?

avatar image siddharth3322 · Jun 25, 2016 at 05:27 PM 0
Share

@Bunny83 can you please give me help in this? ailed to upload image over custom server

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

42 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

Related Questions

Unity networking tutorial? 6 Answers

Best cross platform Http Library? 1 Answer

How to use NetworkManager.ServerChangeScene ? 1 Answer

Network Connection Works Locally, but not Publicly 0 Answers

Running Unity 'headless' on server - Same license? 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