This is a discussion on how to delete last line of file within the PHP Language forums, part of the PHP Programming Forums category; I have a flat file on the server which I append to from a HTML web form. I need to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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($pattern, file('/path/to/your/file.txt')); Kenneth |
|
|||
|
file() returns an array.
See: http://us2.php.net/file_get_contents/ -Mike -- Melt away the Cellulite with Cellulean! http://www.MeltAwayCellulite.com/ "Kenneth" <jagger7774@hotmail.com> wrote in message news:d4f1rf$2ck$1@newsmaster.cc.columbia.edu... > 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($pattern, file('/path/to/your/file.txt')); > > > Kenneth |
|
|||
|
Kenneth wrote:
> 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. <snip> http://in.php.net/fseek#37147 -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/ |
|
|||
|
R. Rajesh Jeba Anbiah wrote:
> Kenneth wrote: > >>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. > <snip> > > http://in.php.net/fseek#37147 > > -- > <?php echo 'Just another PHP saint'; ?> > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/ > 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. |
|
|||
|
Kenneth wrote: > 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. <? $inp = file('yourfile.name'); $out = fopen('yourfile.name','w'); for ($I=0;$i<count($inp)-1);$i++) fwrite($out,$inp[$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 |
|
|||
|
Kenneth wrote:
[snip] > 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. No not really. If you have found the last line, then you are probably pretty close to just do: ftruncate($fp, ftell($fp)); /Bent |
|
|||
|
Kenneth wrote: > Ken Robinson wrote: > > Kenneth wrote: > Ken, I tried your code. Everything fits logically but i keep getting > following error message; > Parse error: syntax error, unexpected ')', expecting ';' in > c:\Inetpub\wwwroot\final\test\lastline.php on line 13 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,$inp[$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 |
|
|||
|
THis is what i have so far.
--------------------------------- $line_counter = 0; $desired_line = 29; $fh = fopen('employees.txt','r+') or die($php_errormsg); while ((! feof($fh)) && ($line_counter <= $desired_line)) { if ($s = fgets($fh,1048576)) { $line_counter++; } } fclose($fh) or die($php_errormsg); print $s; ---------------------------------- Bent Stigsen wrote: > Kenneth wrote: > [snip] > >> 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. > > > No not really. > > If you have found the last line, then you are probably pretty close to > just do: > > ftruncate($fp, ftell($fp)); > > > /Bent |
|
|||
|
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('employees.txt','a+') or die($php_errormsg); while ((! feof($fh)) && ($line_counter <= $desired_line)) { if ($s = fgets($fh,1048576)) { $line_counter++; } } rewind($fh); if (-1 == fwrite($fh,$s)){ die($php_errormsg); } // adjust the file's length to just what's been written ftruncate($fh,ftell($fh)) or die($php_errormsg); // close the file fclose($fh) or die($php_errormsg); print $s; --------------------------------------------------------------------- Kenneth wrote: > 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($pattern, file('/path/to/your/file.txt')); > > > Kenneth |
|
|||
|
Ken Robinson wrote:
> Kenneth wrote: Ken, I tried your code. Everything fits logically but i keep getting following error message; Parse error: syntax error, unexpected ')', expecting ';' in c:\Inetpub\wwwroot\final\test\lastline.php on line 13 But I can't find where there is a syntax error. > >>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. > > > <? > $inp = file('yourfile.name'); > $out = fopen('yourfile.name','w'); > for ($I=0;$i<count($inp)-1);$i++) > fwrite($out,$inp[$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 > |