Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen to add to a file?

Status
Not open for further replies.

Bjelleklang

Programmer
Sep 2, 2003
8
NO
I use $fopen($file, a) to append something to a file, but how do i add something after a spesific tag, or at a specific linenumber??

Also, is it possible to make newlines in a txt file using php???
 
Here's one way to insert text at a specific point:
Read the file either into memory (if the file is small) or to another file (if the file is large), and add the text to the appropriate place.

Then either close the file and write your memory to the file, or delete the old file and rename the new file to the old name.

Keep in mind that this operation can be fraught with problems. You have to make sure that no other script is trying to edit or even read the file while a single instantiation of this script is editing it. Take a look at flock() (


Yes, you can output a new line. Use either:

print "\n"; //for Unix-like OSes

print "\r\n"; //for Win32 OSes

print "\r"; //for some Apple OSes.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top