Page 1 of 1

fwrite is refusing to write variables

Posted: Mon Sep 25, 2006 9:57 am
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,

Posted: Mon Sep 25, 2006 10:41 am
by rsmarsha
Change :

Code: Select all

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

Code: Select all

$write = $_POST["edit"];
and see if it works. :)

Posted: Mon Sep 25, 2006 11:09 am
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.

Posted: Mon Sep 25, 2006 11:16 am
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.

Posted: Mon Sep 25, 2006 11:30 am
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.

Posted: Mon Sep 25, 2006 11:52 am
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);

Posted: Mon Sep 25, 2006 12:03 pm
by impulse()
Yes, I've got PHP5.

I'm having exactly the same problem with file_put_contents.