- Home /
Write to online file with webplayer
I know that the WWW or WWWForm class should be used in conjunction with a server side script such as PHP, but I can not get it to work. I am basically doing this:
var www : WWW;
var form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
www = WWW(url, form); //upload to script
yield www;
Where "url" is a PHP script in the cgi-bin folder on the server where the webplayer is. The PHP script is:
<?php
$filename = 'test.txt';
$Content = "Add this to the file\r\n";
$handle = fopen($filename, 'x+');
fwrite($handle, $Content);
fclose($handle);
?>
I know I am not using "frameCount", but I do not know of an alternative way to put WWW in POST mode. Anyway, this does not produce "test.txt" or write to "test.txt" if I create the file on the server. What am I doing wrong? I know I have access to the PHP script in Unity because I can display its contents in GUIText using the following:
var www : WWW = new WWW (url); var form = new WWWForm();
yield www; // Wait for download to complete
if (www.error != null) this.GetComponent(GUIText).text = www.error; else this.GetComponent(GUIText).text = www.text;
Answer by Tommy · May 17, 2011 at 04:16 PM
If you'r trying to add what Unity sends to the server, maybe you shall do something like this in the PHP script.
<?php
$filename = 'test.txt';
$Content = $_POST['frameCount'];
$handle = fopen($filename,"w");
fwrite($handle, $Content);
fclose($handle);
?>
Note this will probably overwrite whatever's in there, i can add som code for you if you want to ADD the frameCount to the file instead.
Thanks, but this still does not create a file. Also, shouldn't it be fopen($filename, "x+");? I tried fopen("test.txt","x+"); and it still doesn't work. Does it matter whether single or double quotes are used when passing things to a function? I have seen examples both ways. And your fwrite would write the word $Content, not the value, correct? Regardless, this does not create a file. Could it be a permissions thing? I can not see the permissions of the script on the server, but on my local machine it is "full control". Ben 0 secs ago
It won't create a file since you put
$handle = fopen($filename.".txt","x+");
I don't know what the x+ does, but you'll have to use "w" to write to a file, and create a file. Try changing x+ to w. And yes, i belive you should remove the "" from $Content, my bad :-)
I tried your new code and it still does not work. No file is created. If I create test.txt, nothing is written to it. I think the script is probably correct... it must be something else.
Does you webserver have permission to read/write to the folder? Are the script in the same folder as the textfile? Are you looking for it in the same folder? What webserver do you use? What OS are you using?