PHP how to append text to a file?
-
I have a form on my webpage where the visitors select a postcode district from a drop down list and are then presented with the level of service we offer in their area. This works fine, however I want to track what people have selected, when and where they are from? I have written the following code: $file = fopen("test.txt",'a'); $dummy = "Selction is " . $_POST["region"] ." on ". date("F j, Y, g:i a"); fwrite($file,$dummy); fclose($file); Ideally I would like the text file to produce the following: Selection is M on July 5, 2008, 10:17 pm Selection is SK on July 6, 2008, 8:32 am Selection is WA on July 6, 2008, 9:55 am but all I get is the last entry? Any ideas why it is not appending the data correctly? I also want to use the following to add their IP Address: $_SERVER['REMOTE_ADDR'] This does not seem to work either? If I have the IP address, I have found a database I can download for $200 which will allow me to cross reference them, but that is another story....
-
Answer:
It works fine for me? i just tested it on my WAMP server. Ran this with GET instead of POST so i could test from the URL: $filename = "test.txt"; $file = fopen($filename,'a'); $dummy = "Selction is " . $_GET["region"] ." on ". date("F j, Y, g:i a"); $dummy = filesize($filename) > 0 ? "\n".$dummy : $dummy; fwrite($file,$dummy); fclose($file); The line under the first assignment to $dummy is there to basically take a new line if there is already content in the file - though for some reason my notepad has started rendering a line break as a square character :-( regardless i tested it 10 times and got 10 entries in the file. What version of PHP are you using? - Sorry, not sure what else to suggest other than sticking it all in a database? on that note dont pay $200 for something that sounds like it could be written in less than an hour. ;-p
Teddoh at Yahoo! Answers Visit the source
Related Q & A:
- How to find inodes within a file?Best solution by cyberciti.biz
- How to show text inside a progressbar?Best solution by Stack Overflow
- How can I programmatically extract a file quickly and efficiently within Android?Best solution by Stack Overflow
- How do you text from a computer to a Sprint phone?Best solution by eHow old
- How do I text to a cell phone number?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.