Page 1 of 2

Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 5:42 am
by Damian2020
I am trying to fwrite text with HTML formatting to a .txt file on the server. The text is sent from a Flash AS3 form via $_POST.

if ($_POST['sendRequest'] == "update_my_page") {

$my_text = $_POST['my_text'];
$myFile = "myfile.txt";
$myFh = fopen($myFile, 'w') or die("can't open file");
$myString = $my_text;
fwrite($myFh, $myString);
fclose($myFh);
}


This code doesn't rewrite or do anything to the file. I'm an AS3 programmer and new to PHP. Do I need to somehow collect all the data from $_POST and then fwrite it? Or is the data perhaps just not getting to the php file? I feel like I'm missing a step here.

Also, what is the best way to echo/print a message back to the swf to confirm the write?

Thanks.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:02 am
by jackpf
Are you able to see the output from the PHP script?

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:12 am
by Ollie Saunders
Make sure you have error reporting on. You can enable it for this script by sticking these lines:

Code: Select all

ini_set('display_errors', true);
error_reporting(E_ALL);
below the first <?php or <? in your file because TBH you should be getting some sort of error if your code isn't at least creating an empty file.

Other tips:
  • Best not to begin variables with "my".
  • And file_get_contents() achieves what fopen(), fwrite() and fclose() do in a third of the calls.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:14 am
by jackpf
Don't you mean file_put_contents()? ;)

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:33 am
by Ollie Saunders
Yes. Thank you. That is what I mean. :-)

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:47 am
by Damian2020
I don't get to see any output because the php file is being called directly from the swf. It's function is solely to handle this type of request. Even if my error reporting is on, I don't think I'm going to receive the reports. I'm using this archaeic method because I can't figure out how to get my text to keep it's formatting when it goes through a MySQL database, and it's vital that the text keeps it's HTML formatting. (links etc)

Can fwrite() handle a sendRequest through $_POST? Maybe there should be another function/variable in there somewhere to collect all the data before handing it over to the fwrite() function?

Oh and the variables don't actually start with $my, just changed it as a placeholder. But thanks for the tips.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:49 am
by jackpf
Databases can handle html formatting.

And yeah, you can do this.

But you need to be able to see the output of the php script to see if any errors are being thrown, and to check what variables consist of etc.

Can you set this up to run from a normal html form?

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:54 am
by Ollie Saunders
If your SWF can reach the PHP script than you can reach the PHP script in your browser with the same URL. Do that.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:57 am
by jackpf
But he won't have the post data...

You'll have to comment out the "if ($_POST['sendRequest'] == "update_my_page")" line.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 8:59 am
by Ollie Saunders
Good point. You could change to $_REQUEST then and put the values in the query string (?this=that etc.).

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 9:00 am
by Damian2020
At the URL I get one of these for every sendRequest:

Notice: Undefined index: sendRequest ... on line 7

I tried to send the data again with this URL open and then refreshed it but nothing changed.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 9:05 am
by Ollie Saunders
Did you change $_POST to $_REQUEST and put ?sendRequest=whatever_value at the end of the URL? Because that's what you need to do :-)

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 9:12 am
by Damian2020
Ok tried that and this is what I got:

Notice: Undefined index: company_body on line 9

Warning: fopen(company.txt) [function.fopen]: failed to open stream: Permission denied on line 11

can't open file

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 9:13 am
by jackpf
Oh right, that could be the problem. Try chmoding company.txt to 777.

Re: Using fwrite with $_POST variables

Posted: Thu Aug 20, 2009 9:15 am
by Ollie Saunders
Bingo! Check the permissions of the directory you want to write to and all its parents.

N.B. Another bingo -- 3000th post. I think this entitles me to a Mr.Green smiley -> :mrgreen: There we go, lovely.