how to delete last line of file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kenneth

    how to delete last line of file

    I have a flat file on the server which I append to from a HTML web form.
    I need to delete the last line of the flat file before I append though.

    How do i do that?

    I have the following code. I'm not sure if it's correct.

    $pattern = "</member>"; // what I want to look for.
    $ora_books = preg_grep($patt ern, file('/path/to/your/file.txt'));


    Kenneth
  • Guest's Avatar

    #2
    Re: how to delete last line of file

    file() returns an array.

    See: http://us2.php.net/file_get_contents/

    -Mike

    --
    Melt away the Cellulite with Cellulean!



    "Kenneth" <jagger7774@hot mail.com> wrote in message
    news:d4f1rf$2ck $1@newsmaster.c c.columbia.edu. ..[color=blue]
    > I have a flat file on the server which I append to from a HTML web form.
    > I need to delete the last line of the flat file before I append though.
    >
    > How do i do that?
    >
    > I have the following code. I'm not sure if it's correct.
    >
    > $pattern = "</member>"; // what I want to look for.
    > $ora_books = preg_grep($patt ern, file('/path/to/your/file.txt'));
    >
    >
    > Kenneth[/color]


    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: how to delete last line of file

      Kenneth wrote:[color=blue]
      > I have a flat file on the server which I append to from a HTML web[/color]
      form.[color=blue]
      > I need to delete the last line of the flat file before I append[/color]
      though.
      <snip>



      --
      <?php echo 'Just another PHP saint'; ?>
      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

      Comment

      • Kenneth

        #4
        Re: how to delete last line of file

        R. Rajesh Jeba Anbiah wrote:[color=blue]
        > Kenneth wrote:
        >[color=green]
        >>I have a flat file on the server which I append to from a HTML web[/color]
        >
        > form.
        >[color=green]
        >> I need to delete the last line of the flat file before I append[/color]
        >
        > though.
        > <snip>
        >
        > http://in.php.net/fseek#37147
        >
        > --
        > <?php echo 'Just another PHP saint'; ?>
        > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
        >[/color]


        I figured out how to get the last line of the file but I do not know how
        to delete the line without creating a temp file.

        I am assuming it's a long complicated function.

        Comment

        • Ken Robinson

          #5
          Re: how to delete last line of file


          Kenneth wrote:[color=blue]
          > I figured out how to get the last line of the file but I do not know[/color]
          how[color=blue]
          > to delete the line without creating a temp file.
          >
          > I am assuming it's a long complicated function.[/color]

          <?
          $inp = file('yourfile. name');
          $out = fopen('yourfile .name','w');
          for ($I=0;$i<count( $inp)-1);$i++)
          fwrite($out,$in p[$I]);
          fclose($out)l
          ?>

          Not long and complicated at all...

          1) Read file into an array
          2) Open the new file for write
          3) Write all except last line to the new file
          4) Close the new file

          Ken

          Comment

          • Bent Stigsen

            #6
            Re: how to delete last line of file

            Kenneth wrote:
            [snip][color=blue]
            > I figured out how to get the last line of the file but I do not know how
            > to delete the line without creating a temp file.
            >
            > I am assuming it's a long complicated function.[/color]

            No not really.

            If you have found the last line, then you are probably pretty close to
            just do:

            ftruncate($fp, ftell($fp));


            /Bent

            Comment

            • Kenneth

              #7
              Re: how to delete last line of file

              THis is what i have so far.

              ---------------------------------
              $line_counter = 0;
              $desired_line = 29;

              $fh = fopen('employee s.txt','r+') or die($php_errorm sg);
              while ((! feof($fh)) && ($line_counter <= $desired_line)) {
              if ($s = fgets($fh,10485 76)) {
              $line_counter++ ;
              }
              }
              fclose($fh) or die($php_errorm sg);

              print $s;
              ----------------------------------





              Bent Stigsen wrote:[color=blue]
              > Kenneth wrote:
              > [snip]
              >[color=green]
              >> I figured out how to get the last line of the file but I do not know
              >> how to delete the line without creating a temp file.
              >>
              >> I am assuming it's a long complicated function.[/color]
              >
              >
              > No not really.
              >
              > If you have found the last line, then you are probably pretty close to
              > just do:
              >
              > ftruncate($fp, ftell($fp));
              >
              >
              > /Bent[/color]

              Comment

              • Kenneth

                #8
                Re: how to delete last line of file

                Here is what I have but it deletes everything except the first line and
                part of the second line.

                -------------------------------------------------------------------
                $line_counter = 0;
                $desired_line = 29;

                $fh = fopen('employee s.txt','a+') or die($php_errorm sg);



                while ((! feof($fh)) && ($line_counter <= $desired_line)) {
                if ($s = fgets($fh,10485 76)) {
                $line_counter++ ;
                }
                }
                rewind($fh);
                if (-1 == fwrite($fh,$s)) { die($php_errorm sg); }

                // adjust the file's length to just what's been written
                ftruncate($fh,f tell($fh)) or die($php_errorm sg);

                // close the file
                fclose($fh) or die($php_errorm sg);

                print $s;

                ---------------------------------------------------------------------


                Kenneth wrote:[color=blue]
                > I have a flat file on the server which I append to from a HTML web form.
                > I need to delete the last line of the flat file before I append though.
                >
                > How do i do that?
                >
                > I have the following code. I'm not sure if it's correct.
                >
                > $pattern = "</member>"; // what I want to look for.
                > $ora_books = preg_grep($patt ern, file('/path/to/your/file.txt'));
                >
                >
                > Kenneth[/color]


                Comment

                • Kenneth

                  #9
                  Re: how to delete last line of file

                  Ken Robinson wrote:[color=blue]
                  > Kenneth wrote:[/color]
                  Ken, I tried your code. Everything fits logically but i keep getting
                  following error message;
                  Parse error: syntax error, unexpected ')', expecting ';' in
                  c:\Inetpub\wwwr oot\final\test\ lastline.php on line 13


                  But I can't find where there is a syntax error.




                  [color=blue]
                  >[color=green]
                  >>I figured out how to get the last line of the file but I do not know[/color]
                  >
                  > how
                  >[color=green]
                  >>to delete the line without creating a temp file.
                  >>
                  >>I am assuming it's a long complicated function.[/color]
                  >
                  >
                  > <?
                  > $inp = file('yourfile. name');
                  > $out = fopen('yourfile .name','w');
                  > for ($I=0;$i<count( $inp)-1);$i++)
                  > fwrite($out,$in p[$I]);
                  > fclose($out)l
                  > ?>
                  >
                  > Not long and complicated at all...
                  >
                  > 1) Read file into an array
                  > 2) Open the new file for write
                  > 3) Write all except last line to the new file
                  > 4) Close the new file
                  >
                  > Ken
                  >[/color]

                  Comment

                  • Ken Robinson

                    #10
                    Re: how to delete last line of file


                    Kenneth wrote:[color=blue]
                    > Ken Robinson wrote:[color=green]
                    > > Kenneth wrote:[/color]
                    > Ken, I tried your code. Everything fits logically but i keep getting[/color]
                    [color=blue]
                    > following error message;
                    > Parse error: syntax error, unexpected ')', expecting ';' in
                    > c:\Inetpub\wwwr oot\final\test\ lastline.php on line 13[/color]

                    I (obviously) had a few typo's in the code I posted. It should read:
                    <?
                    $inp = file('yourfile. name');
                    $out = fopen('yourfile .name','w');
                    for ($i=0;$i<count( $inp)-1;$i++)
                    fwrite($out,$in p[$i]);
                    fclose($out);
                    ?>

                    You should always take any code posted with a grain of salt and don't
                    just copy it verbatim. Always check for errors in both the syntax and
                    logic.

                    Ken

                    Comment

                    • Bent Stigsen

                      #11
                      Re: how to delete last line of file

                      Kenneth wrote:
                      [snip]

                      Without knowing the size of your file, or how it otherwise is accessed,
                      the generic advice would be to avoid using file() or looping through the
                      whole file with fgets(). And to work on a copy instead of a "live" version.

                      Take a look (again) at the link, which R. Rajesh Jeba Anbiah gave you.
                      The idea is thant you search backwards and look for some marker (which
                      may or may not be a newline, as it is not required for the xml-document
                      to be valid). Once positioned at the right spot in the file, you can
                      start overwriting with new data, perhaps truncating the file first.


                      A quick and dirty way to your specific problem, could be something like...

                      $end_string = "</member>\n";
                      $length_end_str ing = strlen($end_str ing);

                      $fh = fopen('employee s.txt','r+');
                      fseek($fh, -$length_end_str ing, SEEK_END);

                      fwrite($fh, "some string that needs to be added");

                      fwrite($fh, $end_string);
                      fclose($fh);

                      It is of course required that any script modifying the file, all write
                      the same string in the end.


                      /Bent

                      Comment

                      • Kenneth

                        #12
                        Re: how to delete last line of file

                        Ken Robinson wrote:[color=blue]
                        > Kenneth wrote:
                        >[/color]
                        No Ken, you were missing and end bracket.

                        Anyway here is what I have so far. It is working now but I think it
                        takes too much resources. Let me know if you can improve the following
                        code somehow.

                        -----------------------------------------------------------------------------------------

                        //Search for the file
                        $key = "</members>";

                        //load file into $fc array
                        $fc=file("membe rs.xml");

                        //open same file and use "w" to clear file
                        $f=fopen("membe rs.xml","w");

                        //loop through array using foreach
                        foreach($fc as $line)
                        {
                        if (!strstr($line, $key)) //look for $key in each line
                        fputs($f,$line) ; //place $line back in file
                        }
                        fclose($f);

                        -------------------------------------------------------------------------------------------------


                        The problem is that I have to file() which I heard takes up to much
                        resources, but it works. Can I replace the code somehow?




                        [color=blue][color=green]
                        >>Ken Robinson wrote:
                        >>[color=darkred]
                        >>>Kenneth wrote:[/color]
                        >>
                        >>Ken, I tried your code. Everything fits logically but i keep getting[/color]
                        >
                        >[color=green]
                        >>following error message;
                        >>Parse error: syntax error, unexpected ')', expecting ';' in
                        >>c:\Inetpub\ww wroot\final\tes t\lastline.php on line 13[/color]
                        >
                        >
                        > I (obviously) had a few typo's in the code I posted. It should read:
                        > <?
                        > $inp = file('yourfile. name');
                        > $out = fopen('yourfile .name','w');
                        > for ($i=0;$i<count( $inp)-1;$i++)
                        > fwrite($out,$in p[$i]);
                        > fclose($out);
                        > ?>
                        >
                        > You should always take any code posted with a grain of salt and don't
                        > just copy it verbatim. Always check for errors in both the syntax and
                        > logic.
                        >
                        > Ken
                        >[/color]

                        Comment

                        Working...