从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.

drslez4322
drslez4322 我在开始时将PHP和txt文件的权限更改为777只是为了检查它似乎没有解决问题
大约 4 年之前 回复
duancilan5124
duancilan5124 检查您是否有权写入该文件
大约 4 年之前 回复
dongzhang7961
dongzhang7961 好的,谢谢,我会看看
大约 4 年之前 回复
duanjia2772
duanjia2772 谢谢(你的)信息。不幸的是我没有看到任何错误...你可以尝试在php的底部放回一个echo,然后在Unity中读取文本(data_post.text),只是为了确保脚本正确执行万维网。当边打破时,基本上试图缩小范围。
大约 4 年之前 回复
douyin8813
douyin8813 我已经添加了上面的输出
大约 4 年之前 回复
dsadsa123111
dsadsa123111 你可以Debug.Logpost_url变量供我们查看吗?
大约 4 年之前 回复
dongtuoao7987
dongtuoao7987 没有错误,只是没有内容添加到文件中
大约 4 年之前 回复
doukaojie8573
doukaojie8573 你得到的错误是什么?
大约 4 年之前 回复

1个回答

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);
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问