468,315 Members | 1,414 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

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
Jul 17 '05 #1
11 11929
file() returns an array.

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

-Mike

--
Melt away the Cellulite with Cellulean!
http://www.MeltAwayCellulite.com/
"Kenneth" <ja********@hotmail.com> wrote in message
news:d4**********@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

Jul 17 '05 #2
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/

Jul 17 '05 #3
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.
Jul 17 '05 #4

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

Jul 17 '05 #5
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
Jul 17 '05 #6
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


Jul 17 '05 #7
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

Jul 17 '05 #8
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

Jul 17 '05 #9

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

Jul 17 '05 #10
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_string = strlen($end_string);

$fh = fopen('employees.txt','r+');
fseek($fh, -$length_end_string, 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
Jul 17 '05 #11
Ken Robinson wrote:
Kenneth wrote:

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("members.xml");

//open same file and use "w" to clear file
$f=fopen("members.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?


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

Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tor Erik Sønvisen | last post: by
7 posts views Thread by Srinivasa T.N. | last post: by
5 posts views Thread by | last post: by
4 posts views Thread by Ignoramus6539 | last post: by
10 posts views Thread by Joah Senegal | last post: by
reply views Thread by Francesco Pietra | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.