Using fwrite with $_POST variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Damian2020
Forum Newbie
Posts: 10
Joined: Thu Aug 20, 2009 5:25 am

Using fwrite with $_POST variables

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Using fwrite with $_POST variables

Post by jackpf »

Are you able to see the output from the PHP script?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Using fwrite with $_POST variables

Post by jackpf »

Don't you mean file_put_contents()? ;)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post by Ollie Saunders »

Yes. Thank you. That is what I mean. :-)
Damian2020
Forum Newbie
Posts: 10
Joined: Thu Aug 20, 2009 5:25 am

Re: Using fwrite with $_POST variables

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Using fwrite with $_POST variables

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Using fwrite with $_POST variables

Post by jackpf »

But he won't have the post data...

You'll have to comment out the "if ($_POST['sendRequest'] == "update_my_page")" line.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post by Ollie Saunders »

Good point. You could change to $_REQUEST then and put the values in the query string (?this=that etc.).
Damian2020
Forum Newbie
Posts: 10
Joined: Thu Aug 20, 2009 5:25 am

Re: Using fwrite with $_POST variables

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post 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 :-)
Damian2020
Forum Newbie
Posts: 10
Joined: Thu Aug 20, 2009 5:25 am

Re: Using fwrite with $_POST variables

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Using fwrite with $_POST variables

Post by jackpf »

Oh right, that could be the problem. Try chmoding company.txt to 777.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Using fwrite with $_POST variables

Post 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.
Post Reply