delete one line of a file??? 
Author Message
 delete one line of a file???

Is PHP able to delete one single line of a file (the last one)
I am creating a webpage, that should create a log-file with user-defined
form-entries...
Now I put a "back-button" into the page and I want the page to delete the
last written line, if the back-button was pressed. In SELFPHP I found
"ftruncate" to shorten a file to a special lengh, but I don't know the
length of the file because it's user-defined. Does anyone know a solution?
-- mfg
Fred -- -----------------------------------------------------------------


Mon, 20 Jun 2005 20:11:07 GMT  
 delete one line of a file???

Quote:

> Is PHP able to delete one single line of a file (the last one)

No, but the programmer is, using the functions available via the PHP API ;).

...

Quote:
> Now I put a "back-button" into the page and I want the page to delete the
> last written line, if the back-button was pressed. In SELFPHP I found
> "ftruncate" to shorten a file to a special lengh, but I don't know the
> length of the file because it's user-defined. Does anyone know a solution?

One approach is the read the file into an array using:

$array = file( 'myfile' );
array_pop( $array ); # removes last item

then write $array back to your file using fopen/fwrite().

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.



Mon, 20 Jun 2005 22:40:05 GMT  
 delete one line of a file???
Quote:

> > Is PHP able to delete one single line of a file (the last one)

> No, but the programmer is, using the functions available via the PHP API

;).
---
Hehehe...funny...I never laughed so much about a joke :)))

Quote:
> > Now I put a "back-button" into the page and I want the page to delete
the
> > last written line, if the back-button was pressed. In SELFPHP I found
> > "ftruncate" to shorten a file to a special lengh, but I don't know the
> > length of the file because it's user-defined. Does anyone know a
solution?

> One approach is the read the file into an array using:

> $array = file( 'myfile' );
> array_pop( $array ); # removes last item

> then write $array back to your file using fopen/fwrite().

Interresting solution, I will try it. Seems to work...


Mon, 20 Jun 2005 20:31:52 GMT  
 delete one line of a file???


Quote:
> Is PHP able to delete one single line of a file (the last one)
> I am creating a webpage, that should create a log-file with user-defined
> form-entries...
> Now I put a "back-button" into the page and I want the page to delete the
> last written line, if the back-button was pressed. In SELFPHP I found
> "ftruncate" to shorten a file to a special lengh, but I don't know the
> length of the file because it's user-defined. Does anyone know a solution?
> -- mfg

You could use file() to load the file in question into an array, unset() the
last array entry and then save the array out to disk (You'll have to write
your own function for that), but I don't know, this seems a little clumsy.
I think web servers such as Apache could do what you want with Server Side
Includes.


Fri, 24 Jun 2005 21:37:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to replace one or two words with one word with one line of awk code

2. bug(?): menu delete operation always deletes at least one entry

3. deleting newline from last line in file

4. Deleting lines in a file

5. how to delete the a line in a file

6. Deleting and appending lines in ASCII files

7. Deleting a number of lines from a .txt file

8. deleting top line from a file

9. Preserving EAs when deleting lines from a file.

10. Delete a certain line from a file

11. delete lines in file

12. Deleting first line of a file

 

 
Powered by phpBB® Forum Software