how to delete last line of file

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2005
Kenneth
 
Posts: n/a
Default 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($pattern, file('/path/to/your/file.txt'));


Kenneth
Reply With Quote
  #2 (permalink)  
Old 04-24-2005
 
Posts: n/a
Default 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!
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



Reply With Quote
  #3 (permalink)  
Old 04-24-2005
R. Rajesh Jeba Anbiah
 
Posts: n/a
Default Re: how to delete last line of file

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/

Reply With Quote
  #4 (permalink)  
Old 04-25-2005
Kenneth
 
Posts: n/a
Default Re: how to delete last line of file

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.
Reply With Quote
  #5 (permalink)  
Old 04-25-2005
Ken Robinson
 
Posts: n/a
Default Re: how to delete last line of file


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

Reply With Quote
  #6 (permalink)  
Old 04-25-2005
Bent Stigsen
 
Posts: n/a
Default Re: how to delete last line of file

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
Reply With Quote
  #7 (permalink)  
Old 04-25-2005
Ken Robinson
 
Posts: n/a
Default Re: how to delete last line of file


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

Reply With Quote
  #8 (permalink)  
Old 04-25-2005
Kenneth
 
Posts: n/a
Default Re: how to delete last line of file

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


Reply With Quote
  #9 (permalink)  
Old 04-25-2005
Kenneth
 
Posts: n/a
Default 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('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



Reply With Quote
  #10 (permalink)  
Old 04-25-2005
Kenneth
 
Posts: n/a
Default Re: how to delete last line of file

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
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0