Jump to content

Implimenting code into fwrite correctly


Freedom-n-Democrazy

Recommended Posts

<?php
$fp = fopen ('data.txt', 'w');
fwrite($fp, $_POST['feedback']);
fclose($fp);
echo 'Done.';
?>

 

Why have I implemented $_POST['feedback'] incorrectly? I also tried:

 

".$_POST['feedback']."

'".$_POST['feedback']."'

'.$_POST['feedback']'

.$_POST['feedback']

Link to comment
https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/
Share on other sites

Tried to work around with this, but failed. In fact, PHP doesn't even touch the file!  :shrug:

 

<?php

$file = "data.txt";

$data = $_POST['feedback'];

if ($_POST['feedback']) file_put_contents ($data, $data);

echo 'Done.';

?>

Hmm one last thought. Do you have the correct permissions set on the directory the text file is in?

 

Try:

chmod 777 /whatever/directory/your/in 

 

Also chmod 777 the actual file, but directory if PHP is generating the file for you.

 

Also in your above code you never gave the correct arguments :P 

You gave

file_put_contents($data, $data)

instead of

file_put_contents($file, $data)

 

If your going to use file_put_contents I would also suggest using LOCK_EX.

 

file_put_contents($file, $data, LOCK_EX)

 

Hope this helps :)

 

--LiquidFusi0n

This guy seems to have the same problem, which enforces my believe this is a bug PHP.

http://www.phpbuilder.com/board/showthread.php?t=10318000

 

I tried to use his solution, but it failed:

 

<?php
$data = $_POST['feedback'];
if ($_POST['feedback']) file_put_contents ('data.txt', $data);
echo 'Done.';
?>

It re-enforces yes... but I like one person on that forum have successfully done this before.... So don't believe it to be a PHP bug.

 

So it may be a machine/set-up specific bug... I'm not sure I will try it out fully tomorrow with both functions.

 

--LiquidFusi0n

Well I can now 100% say it's not a PHP bug as I have just written a working code.

 

   1 <html>
  2 <body>
  3 
  4 <form method="POST" action="test.php">
  5 Feedback: <input type="text" name="feedback">
  6 <input type="submit" value="Feedback" name="submit">
  7 
  8 <?php
  9 
10 if(isset($_POST['submit']) && isset($_POST['feedback'])){
11         $file = "test.txt";
12         $data = $_POST['feedback'];
13         file_put_contents ($file, $data);
14         echo "Done";
15 }
16 
17 ?>
18 
19 </body>
20 </html>

 

So now we know it is a machine/code specific problem hmmmmm

 

--LiquidFusi0n

error.log states:

 

PHP Notice:  Undefined index: feedback

 

^^^ means $_POST['feedback'] doesn't exist.

 

Add this to see what actually does exist in the $_POST array:

echo 'Post array:<br><pre>';
print_r($_POST);
echo '</pre>';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.