Upload txt file to server using php

Hi,

I’m trying to upload a text file to a server but I keep getting a
“There was an error saving data: 500 Internal Server Error”

In Unity I use this script named SaveOnline:

using UnityEngine;
using System.Collections;
 
public class SaveOnline : MonoBehaviour {
 
    public string postDataURL = "http://magneticstudio.com/save_player_data.php?"; //add a ? to your url
       
        public string Playername;
        public string Playerdata;
       
    void Start()
    {
                Playername = "PlayerName";
                Playerdata = "SomeData";
        StartCoroutine(PostData(Playername,Playerdata));
    }
 
    IEnumerator PostData(string name, string data)
    {
        //This connects to a server side php script that will write the data
         string post_url = postDataURL + "name=" + WWW.EscapeURL(name) + "&data=" + data ;
 
        // Post the URL to the site and create a download object to get the result.
        WWW data_post = new WWW(post_url);
        yield return data_post; // Wait until the download is done
 
        if (data_post.error != null)
        {
            print("There was an error saving data: " + data_post.error);
        }
    }
}

And on the server I have this php file named
save_player_data.php

<?php
        $name_to_save = $_GET["name"];
        $data_to_save = $_GET["data"];
       
        $myFile = $name_to_save . ".txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh,$name_to_save . "," . $data_to_save . ",");
        fclose($fh);
?>

This setup used to work but now doesn’t. I’m using Unity5
Please help me someone:)
Peter

“500 internal server error” is an HTTP response code that basically means that something went wrong on the server (specifically, something went wrong in the PHP, not in unity).

Check the error logs of the web server and you’ll probably see PHP errors that tell you what’s going on. Here are a few guesses, though:

  • The file can’t be opened (perhaps due to permissions on the directory)
  • The filename is invalid (since the player name isn’t being escaped).
  • The disk is full or you’ve reached your webhost’s disk quota

This example worked perfectly fine. Hope it helps.
https://forum.unity.com/threads/need-help-with-scripting-php-for-uploading-txt-files-to-http-server.466039/