duankezong4064 2016-04-08 14:00
浏览 164

从Unity写入服务器上的Text文件

Hi I have been making an app in Unity and I have been trying to add code that will log User actions i.e. they have made a certain game object appear. What I am trying to do is write to a .txt file stored on a server I have the following code which is based on:

http://answers.unity3d.com/questions/984290/upload-txt-file-to-server-using-php.html

and they suggest it works, for me it is not writing anything to the file.

Here is my PHP code (SaveData.php):

<?php
         $Action = $_GET["action"];
         $Date = date('Y/m/d H:i:s');

         $myFile = "TestingData.txt";
         $fh = fopen($myFile, 'a') or die("can't open file");
         fwrite($fh,$Action . "," . $Date . "
");
         fclose($fh);
 ?>

And here is the C# (SaveData.cs) I am attaching to the game objects I change the Action value in the inspector window:

using UnityEngine;
using System.Collections;

public class SaveData : MonoBehaviour {

public string postDataURL = "http://arinstructionman.byethost7.com/SaveData.php?"; //add a ? to your url

         public string Action;

     void Start()
     {

         StartCoroutine(PostData(Action));
     }

     IEnumerator PostData(string action)
     {

         string post_url = postDataURL + "action=" + WWW.EscapeURL(action);

         WWW data_post = new WWW(post_url);

         yield return data_post;

         if (data_post.error != null)
         {
             print("There was an error saving data: " + data_post.error);
         }
     } 
 }

If I copy the post_url value and run it in the browser it seems to work fine, can anyone advise what I am doing wrong?

post_url output:

http://arinstructionman.byethost7.com/SaveData.php?action=Plane+Visible

Any help is much appreciated.

  • 写回答

1条回答 默认 最新

  • dsff788655567 2016-08-15 16:12
    关注

    I would suggest trying this with $_POST instead of $_GET and see if you get a different response.
    Also, you can use file_put_contents to open, write, and close the file all in one line, and using the FILE_APPEND flag to prevent overwriting the existing file. If the file does not exist, this will also create the file for you.

    <?php
        if(!isset($_POST["action"])) 
        {
            echo ("NO DATA RECIEVED");
            return;
        }
    
        $action = $_POST["action"];
        $date = date('Y/m/d H:i:s');
        $data = "{$action},{$date}
    ";
        $myFile = "TestingData.txt";
    
        if(file_put_contents($myFile, $data, FILE_APPEND))
            echo ("success");
        else
            echo ("failed to write file);
     ?>
    

    And use WWWform to POST data to the PHP file:

    public string postDataURL = "http://arinstructionman.byethost7.com/SaveData.php";
    public string Action = "some action";
    
    void Start()
    {
        StartCoroutine(PostData(Action));
    }
    
    IEnumerator PostData(string action)
    {
        WWWForm form = new WWWForm();
        form.AddField("action", action);
        WWW dataPost = new WWW(postDataURL, form);
    
        yield return dataPost;
    
        if (dataPost.error != null)
            print("There was an error saving data: " + data_post.error);
        else
            print(dataPost.text);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题