Appending to a .txt file. |
Appending to a .txt file. |
Jester335 |
Dec 29 2008, 10:22 PM
Post
#1
|
Group: Members Posts: 6 Joined: 29-December 08 Member No.: 7,419 |
I need to know how to write the contents of a textbox to a .txt file without overwriting the text currently in the file, preferably with PHP. Can anyone help me out?
This post has been edited by Jester335: Dec 29 2008, 10:23 PM |
Darin McGrew |
Dec 30 2008, 02:07 AM
Post
#2
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
Are you trying to append the new data to the existing file? Or are you trying to create a new file each time the form is submitted?
|
Jester335 |
Dec 30 2008, 02:37 AM
Post
#3
|
Group: Members Posts: 6 Joined: 29-December 08 Member No.: 7,419 |
|
Darin McGrew |
Dec 30 2008, 02:39 AM
Post
#4
|
WDG Member Group: Root Admin Posts: 8,365 Joined: 4-August 06 From: Mountain View, CA Member No.: 3 |
How are you opening the file now?
|
Jester335 |
Dec 30 2008, 02:50 AM
Post
#5
|
Group: Members Posts: 6 Joined: 29-December 08 Member No.: 7,419 |
CODE $myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); I should have been more specific, I am not looking for the PHP code to append a text file with new data, but rather, looking for an answer on how to execute the PHP code when the "Submit" button is pressed, I usually deal with ASP and ASP.NET, but the host my friend is using doesn't support either, so I am forced to resort to PHP. |
Brian Chandler |
Dec 30 2008, 04:51 AM
Post
#6
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
CODE $myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); I should have been more specific, I am not looking for the PHP code to append a text file with new data, but rather, looking for an answer on how to execute the PHP code when the "Submit" button is pressed, I usually deal with ASP and ASP.NET, but the host my friend is using doesn't support either, so I am forced to resort to PHP. Your bit of program looks ok -- at least as far as answering the question in your subject line -- but if you want to process the result of submitting a form, you need to get the values passed in the GET or POST request, using arrays $_GET and $_POST respectively. Where is the form being submitted? Can you give us a URL, or the form code...? BTW, your "write to file" stuff should really lock the file, just in case two instances of your form get submitted at the same time. Something like (no guarantees) my error logging function that writes to the end of the current month's error log: function errlog($msg) // writes error with referrer etc. (new file each month) { $mm=date("n"); // this month (1 - 12) $absfile = $_SERVER["DOCUMENT_ROOT"] . "/shop/admin/err$mm.txt"; $log = fopen($absfile, "a"); flock($log, LOCK_EX); fwrite($log, jtime() . " JST *** $msg\n"); fwrite($log, $_SERVER["REQUEST_URI"]); if ($_SERVER["HTTP_REFERER"]) fwrite($log, " from " . $_SERVER["HTTP_REFERER"]); fwrite($log, "\n" . $_SERVER["HTTP_USER_AGENT"] . "\n\n"); flock($log, LOCK_UN); } HTH |
Christian J |
Dec 30 2008, 05:16 AM
Post
#7
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
There's a basic PHP tutorial on how forms here: http://www.php.net/manual/en/tutorial.forms.php
From PHP5 you can use http://www.php.net/manual/en/function.file-put-contents.php to write to files. |
Lo-Fi Version | Time is now: 25th April 2024 - 10:20 PM |