The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Appending to a .txt file.
Jester335
post 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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post 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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jester335
post Dec 30 2008, 02:37 AM
Post #3





Group: Members
Posts: 6
Joined: 29-December 08
Member No.: 7,419



QUOTE(Darin McGrew @ Dec 30 2008, 02:07 AM) *

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?


Appending the new data to an existing file.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post 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?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Jester335
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Dec 30 2008, 04:51 AM
Post #6


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE(Jester335 @ Dec 30 2008, 04:50 PM) *

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
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post 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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 07:34 AM