p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

beginning_php thread: Re: Writing data to a file and delimiting seperate data with a line break.

Message #1 by "Tim" <timw@g...> on Mon, 20 May 2002 11:58:09

Probably by now you've figured out this problem.  But since I just 
finished getting to the bottom of it in my own application, I figured I'd 
pass along the resolution (maybe it'll help someone else out there).

Anyway, the problem is you need to open the file in "append" mode.  So, 
instead of using "write" mode, as in:

$fp = fopen("filename.txt", "w");

Use append mode:

$fp = fopen("filename.txt", "a");

The unexpected characteristic of the "w" write mode is that it first 
deletes the contents fo the file before writing the new data.  "Append" 
mode does exactly what it says... "Open the file and append to the bottom 
of it."

By the way - Using "a+" will give read and append access to the file.  

If you need to add carriage returns, new line characters, or tab 
characters to your text file, it's coded using \r, \n, \t respectively.  
Search for info on "escaping characters" for more info on this.

So long,

Original Message Follows

I would like to take data input from a form and have it write to a file on 
the server. Compiling and arranging the data for writing the file I find 
simple. My problem is taking each seperate input and writing that data to 
a newline. I have tried several combinations of file functions but do not 
achieve the desired results. Is there a way to tell PHP that if there is 
data on a file to find the end of it and go to the next line before 
writing the new data?

The book Beginning PHP talks about CR LF to invoke a newline(for win32 
plat) but it does not say how to use it. I find this all very confusing.

  Return to Index