Loading ...
Sorry, an error occurred while loading the content.

File append problem

Expand Messages
  • Boby George
    Is it possible to add a line just before the last line in a text file by using PHP code? Thanks Yahoo! Mail Stay connected, organized, and protected. Take the
    Message 1 of 3 , May 5, 2005
    • 0 Attachment
      Is it possible to add a line just before the last line
      in a text file by using
      PHP code?

      Thanks



      Yahoo! Mail
      Stay connected, organized, and protected. Take the tour:
      http://tour.mail.yahoo.com/mailtour.html
    • Nirav Mehta
      Hi, ... Of course it is possible ;-) Use the file() function to read the contents of the file in an array. Then modify the array and save it back to the file
      Message 2 of 3 , May 5, 2005
      • 0 Attachment
        Hi,

        > Is it possible to add a line just before the last line
        > in a text file by using
        > PHP code?

        Of course it is possible ;-)

        Use the file() function to read the contents of the file in an array. Then
        modify the array and save it back to the file by imploding the array with
        new line - "\n" character.

        Something like this:

        $contents = file("filename.txt");
        $lines = count($contents);
        $contents[$lines+1] = $contents[$lines];
        $contents[$lines] = "New content";

        Best Regards,
        Nirav
      • satish mantri
        hello, There is no direct API but if you can keep moving to next line and reading all the data in a buffer then you can manipulate that buffer and over write
        Message 3 of 3 , May 5, 2005
        • 0 Attachment
          hello,
          There is no direct API but if you can keep moving to
          next line and reading all the data in a buffer then
          you can manipulate that buffer and over write the
          whole file.
          If the file is big just claculate the number of lines
          applying some character recoginition.Then save the
          last line in the buffer.
          After this you move yourself to the last but one line
          and paste any new comment followed by what you have
          saved in the last line.
          Better way would to write a class and that will handle
          it.

          --- Boby George <bob_by19@...> wrote:
          > Is it possible to add a line just before the last
          > line
          > in a text file by using
          > PHP code?
          >
          > Thanks
          >
          >
          >
          > Yahoo! Mail
          > Stay connected, organized, and protected. Take the
          > tour:
          > http://tour.mail.yahoo.com/mailtour.html
          >
          >
          >
          >



          __________________________________
          Do you Yahoo!?
          Yahoo! Small Business - Try our new resources site!
          http://smallbusiness.yahoo.com/resources/
        Your message has been successfully submitted and would be delivered to recipients shortly.