Hi!
I try to read and write a simple textfile from a server. Reading works, but I can't get it to write. The result in OnWriteRequestResult is a success, but it doesn't add the line to the file. Does someone know why?
using Godot;
using System;
public partial class TopListController : Node2D
{
private const string wUrl = "https://myserver.com/server/w_toplist.php";
private const string rUrl = "https://myserver.com/server/r_toplist.php";
public override void _Ready()
{
string test = "Name: " + "name" + "\n" + "Mail: " + "email" + "\n" + "Score: " + "60000";
writeToplist(test);
readToplist();
}
public void writeToplist(string data) {
HttpRequest request = new HttpRequest();
AddChild(request);
request.RequestCompleted += (result, response_code, headers, body) => { OnWriteRequestCompleted(result, response_code, headers, body); };
request.Request(wUrl, new string[] {"toplist_data=" + data}, HttpClient.Method.Post);
}
public void readToplist() {
HttpRequest request = new HttpRequest();
AddChild(request);
request.RequestCompleted += (result, response_code, headers, body) => { OnReadRequestCompleted(result, response_code, headers, body); };
request.Request(rUrl);
}
private void OnReadRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
{
if (result != (int)HttpRequest.Result.Success)
{
// Error handling
GD.Print("HTTP request failed");
return;
}
string responseString = System.Text.Encoding.UTF8.GetString(body);
GD.Print(responseString);
}
private void OnWriteRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
{
if (result == (int)HttpRequest.Result.Success)
{
GD.Print("Entry written to server");
return;
}
GD.Print("Failed to write on server");
}
}
and the php files:
<?php
$toplist_data = $_POST['toplist_data'];
$file = fopen("toplist.txt", "w");
fwrite($file, $toplist_data);
fclose($file);
?>
<?php
$file = fopen("toplist.txt", "r");
$toplist_data = fread($file, filesize("toplist.txt"));
fclose($file);
echo $toplist_data;
?>
Thank you very much in advance!