Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Upload file to a http server - WWWForm + PHP

Discussion in 'Editor & General Support' started by castor, Dec 1, 2015.

  1. castor

    castor

    Joined:
    Sep 9, 2012
    Posts:
    40
    I've been banging my head on this problem for 3 days now and I'm sure anyone with a basic knowledge of PHP should be able to easily help, so here goes!

    In my current project, a textfile (userdata.log) gets written as you play the game. What I'm trying to do, is upload this file to a server at certain key moments of the game.

    To do so, I've followed this other thread that teaches how to use 2 simple scripts to upload levels. One lever uploader script in Unity and one LevelUploader.php file that is placed on the server. (Also, as suggested I'm using free webhosting at http://x10hosting.com/)

    Also, I'm pretty sure the problem is in the server settings, since when I try to upload the file I get a 500 Internal Server Error...but I know nothing about PHP, server permission, etc, so any help very appreciated!

    Here are the details.

    This is the script in Unity:
    Code (JavaScript):
    1. function UploadLog(){
    2.     var logData : byte[] = System.IO.File.ReadAllBytes(worldInteractions.logFile);
    3.     var logName : String = Path.GetFileName(worldInteractions.logFile);
    4.  
    5.     var form : WWWForm = new WWWForm();
    6.         form.AddField("path", "playerLogs");
    7.         form.AddField("frameCount", Time.get_frameCount().ToString());
    8.         form.AddBinaryData("file", logData, logName);
    9.      
    10.         www = new WWW("http://mydomainname.x10host.com/upload_file.php", form);
    11.         yield www;
    12.      
    13.         if (!String.IsNullOrEmpty(www.error)){
    14.             Debug.Log("UPLOAD ERROR: " + www.url + "[" + www.error + "]");
    15.         }
    16.         else {
    17.             Debug.Log("UPLOAD SUCCESSFUL [" + logName + "]");
    18.         }  
    19. }
    And here is the upload_file.php file:

    Code (JavaScript):
    1. <?php
    2.     if (!empty($_FILES) && !empty($_POST) && !empty($_POST["path"])){
    3.            if ($_FILES["file"]["error"] > 0){
    4.                  echo "Return Code: " . $_FILES["file"]["error"] . "";
    5.            }
    6.            else {
    7.             // Desired folder structure
    8.             $structure = "/" . $_POST["path"];
    9.  
    10.             if (!file_exists($structure)){
    11.                 // To create the nested structure, the $recursive parameter
    12.                 // to mkdir() must be specified.
    13.  
    14.                 if (!mkdir($structure, 0777, true)){
    15.                         echo "Unable to create directory " . $structure;
    16.                 }
    17.             }
    18.                  
    19.                      if (file_exists($structure . "/" . $_FILES["file"]["name"])){
    20.                              echo $_FILES["file"]["name"] . " already exists. ";
    21.                      }
    22.                       else{
    23.                        move_uploaded_file($_FILES["file"]["tmp_name"], $structure . "/" . $_FILES["file"]["name"]);
    24.                 echo "Uploaded to " . $structure . "/" . $_FILES["file"]["name"];
    25.                       }
    26.         }
    27.     }
    28.     else{
    29.         echo "No file or path to save.";
    30.     }
    31. ?>
    As for the hosting, here is where the file was placed:
    FolderLayout.jpg

    I've also tried to change folder/file permissions, adding a .htaccess file as well as a crossdomain.xml, but I'm totally ignorant about this stuff so...yeah, any help would be amazing!
     
  2. allenwp

    allenwp

    Joined:
    Sep 4, 2013
    Posts:
    46
    Seems that Unity's WWWForm sends as POST, which is what your PHP is parsing, so that's a good start. I don't know off-hand what's wrong (since I've never worked with WWW before), but I think the first thing to do is remove possible problems and simplify it as much as you can before building it up to include a binary blob sent from Unity.

    So the first thing I would do is take Unity out of the equation altogether and make sure your server side is working as you expect. The reason I think this will be helpful is because other tools, like web browsers with REST clients are designed to give you as much transparency about what's going on as possible. I don't know what browser you're using, but a quick google search brings up lots of options: http://superuser.com/questions/345034/google-chrome-extension-to-submit-post-get-requests . You can also bring up your development console to see data as it's being sent by the browser (F12 in firefox and chrome).

    First start by sending out just one field as plain text and try and get that working. Then add a second field. Then try to get that working with Unity's WWWForm. Then go back to the rest client and try to send a binary blob. Then try and get that working in Unity.

    Might be good if you started with a simpler POST PHP just to get the hang of debugging:

    Code (php):
    1. <?phpecho 'Hello ' . htmlspecialchars($_POST["name"]) . '!';?>
    (link)

    Sorry that's not immediately helpful, but hopefully that leads you in the right direction...
     
  3. nre3d

    nre3d

    Joined:
    Sep 18, 2017
    Posts:
    2