Freedom-n-Democrazy Posted September 26, 2011 Share Posted September 26, 2011 <?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'] Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/ Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 Looks like it should work... matches the correct syntax for the command. You may not want to but just try storing it to to a variable and passing fwrite that instead. --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272746 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 Exactly. Try it out yourself. It will produce a blank data.txt file, but if you change $_POST['feedback'] to "foobar" it works. Bug? Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272750 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 Tried to work around with this, but failed. In fact, PHP doesn't even touch the file! <?php $file = "data.txt"; $data = $_POST['feedback']; if ($_POST['feedback']) file_put_contents ($data, $data); echo 'Done.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272751 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272753 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 Yes. Exactly. Try it out yourself. It will produce a blank data.txt file, but if you change $_POST['feedback'] to "foobar" it works. Bug? Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272755 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 Also tried but failed: <?php $fp = fopen ('data.txt', 'w'); $data = $_POST['feedback']; file_put_contents ($fp, $data); fclose($fp); echo 'Done.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272757 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 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.'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272758 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272759 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 error.log states: PHP Notice: Undefined index: feedback Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272760 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272762 Share on other sites More sharing options...
Pikachu2000 Posted September 26, 2011 Share Posted September 26, 2011 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272764 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 LOL I'm a goose! Left out name="feedback" in the HTML. LOL OMG sorry guys. Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272766 Share on other sites More sharing options...
Freedom-n-Democrazy Posted September 26, 2011 Author Share Posted September 26, 2011 ... and you were both right! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272768 Share on other sites More sharing options...
LiquidFusi0n Posted September 26, 2011 Share Posted September 26, 2011 That's twice in one day that's been the solution.... It was the solution to my own problem but I forgot a " No problem, glad you got it sorted --LiquidFusi0n Quote Link to comment https://forums.phpfreaks.com/topic/247864-implimenting-code-into-fwrite-correctly/#findComment-1272770 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.