Upload file to a http server - WWWForm + PHP

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:

function UploadLog(){
    var logData : byte[] = System.IO.File.ReadAllBytes(worldInteractions.logFile);
    var logName : String = Path.GetFileName(worldInteractions.logFile);
  
    var form : WWWForm = new WWWForm();
        form.AddField("path", "playerLogs");
        form.AddField("frameCount", Time.get_frameCount().ToString());
        form.AddBinaryData("file", logData, logName);
      
        www = new WWW("http://mydomainname.x10host.com/upload_file.php", form);
        yield www;
      
        if (!String.IsNullOrEmpty(www.error)){
            Debug.Log("UPLOAD ERROR: " + www.url + "[" + www.error + "]");
        }
        else {
            Debug.Log("UPLOAD SUCCESSFUL [" + logName + "]");
        }   
}

And here is the upload_file.php file:

<?php
    if (!empty($_FILES) && !empty($_POST) && !empty($_POST["path"])){
           if ($_FILES["file"]["error"] > 0){
                 echo "Return Code: " . $_FILES["file"]["error"] . "";
           }
           else {
            // Desired folder structure
            $structure = "/" . $_POST["path"];
  
            if (!file_exists($structure)){
                // To create the nested structure, the $recursive parameter
                // to mkdir() must be specified.
  
                if (!mkdir($structure, 0777, true)){
                        echo "Unable to create directory " . $structure;
                }
            }
                  
                     if (file_exists($structure . "/" . $_FILES["file"]["name"])){
                             echo $_FILES["file"]["name"] . " already exists. ";
                     }
                      else{
                       move_uploaded_file($_FILES["file"]["tmp_name"], $structure . "/" . $_FILES["file"]["name"]);
                echo "Uploaded to " . $structure . "/" . $_FILES["file"]["name"];
                      }
        }
    }
    else{
        echo "No file or path to save.";
    }
?>

As for the hosting, here is where the file was placed:
2404142--164153--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!

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: Google Chrome Extension to submit POST/GET requests? - Super User . 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:

<?phpecho 'Hello ' . htmlspecialchars($_POST["name"]) . '!';?>

(link)

Sorry that’s not immediately helpful, but hopefully that leads you in the right direction…

This worked for me.