fwrite is refusing to write 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

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

fwrite is refusing to write variables

Post by impulse() »

I modified this code from the internet:

Code: Select all

<?php
$File = "YourFile.txt";
$Handle = fopen($File, 'w');
$Data = "Jane Doe\n";
fwrite($Handle, $Data);
$Data = "Bilbo Jones\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
Which works dandy. But if I modify it to use my code:

Code: Select all

<form method="post"
      action="write.php">
<input type="text"
       name="edit">
<input type="submit"
       value="Write!">
</form>

<?php
$file = "file.txt";
$do = fopen($file, 'w');
$write = $_REQUEST["edit"];
fwrite($do, $write);
echo "<br><br>Inserted $write<br>";
fclose($do);
?>
Yet if I hardcode the data to insert (

Code: Select all

$write = "teapot";
) it works fine. But as soon as I use post to get the data from a HTML form it refuses to insert it. But if I echo the variable $write after it has been posted from a HTML form it echos fine.

Regards,
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Change :

Code: Select all

$write = $_REQUEST["edit"];
to

Code: Select all

$write = $_POST["edit"];
and see if it works. :)
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Didn't work. I don't know the difference between $_REQUEST & $_POST but I can echo out the variable that I get from $_REQUEST so the problem must lay within the fwrite function of not posting a variable that's been passed from a HTML form. I can't get my head around why though. If I do

Code: Select all

$windowLicker = "I've been inserted";
and then use that with fwrite it works fine.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

impulse() wrote:Didn't work. I don't know the difference between $_REQUEST & $_POST
$_POST contains only variables submitted via post (IE: <form method="post") $_REQUEST is a composition of the superglobal arrays $_POST (which contains posted data), $_GET (ie: <form method="get"> or http://yoursite.com/index.php?this=a_get_variable) and $_COOKIE which is data pulled from a user cookie.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

It's as if fwrite is clever enough to know that the data contained within the variable I want it to write was passed into the variable from a HTML text box and therefor is refusing to write it. If I hardcode the same data into the same variable name it works fine.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post by panic! »

are you using php5?

if so have you considered using file_put_contents...

its abit simpler, theres no handlers etc.


file_put_contents("file.txt","$write");

or to append:

file_put_contents("file.txt","$write",FILE_APPEND);
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Yes, I've got PHP5.

I'm having exactly the same problem with file_put_contents.
Post Reply