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:
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!