sataiuto Posted October 18, 2014 Share Posted October 18, 2014 How to do it? I have read some methods but they seem to not work. This is what I have got so far: <?php $name= $_GET["name"]; $comment= $_GET["comment"]; $f = fopen('data.txt', 'a'); fwrite($f, "<p>$name and $comment</p><hr>"); fclose($f); ?> So, basically I want to write certain data from the input form to a text file. ATM, the server creates the data.txt but doesnt write anything on it. The $name and $comment variables are working if I echo on submit. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/291912-writing-get-content-from-formphp-file-to-a-separate-texttxt/ Share on other sites More sharing options...
Solution jcbones Posted October 18, 2014 Solution Share Posted October 18, 2014 That code should work, as I see nothing systematically wrong with it. file_put_contents() is easier, and less coding. $name = trim($_GET['name']); $comment = trim($_GET['comment']); if(file_put_contents('data.txt',"<p>$name and $comment</p><hr />\n",FILE_APPEND) !== false) { echo 'File was written successfully!'; } else { echo 'The file failed to write.'; } Quote Link to comment https://forums.phpfreaks.com/topic/291912-writing-get-content-from-formphp-file-to-a-separate-texttxt/#findComment-1494104 Share on other sites More sharing options...
Frank_b Posted October 18, 2014 Share Posted October 18, 2014 exactly file_put_contents is easier. And like jcbones showed you really have to show an error if the file was not written since there can be enough reasons that the function could fail. One of the reasons is permisions on a linux server. Give your directory where you want to write to enough permissions (755). Quote Link to comment https://forums.phpfreaks.com/topic/291912-writing-get-content-from-formphp-file-to-a-separate-texttxt/#findComment-1494106 Share on other sites More sharing options...
sataiuto Posted October 18, 2014 Author Share Posted October 18, 2014 Thanks a lot guys Quote Link to comment https://forums.phpfreaks.com/topic/291912-writing-get-content-from-formphp-file-to-a-separate-texttxt/#findComment-1494108 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.