fgets() PHP read last line?
Using the fgets() function (or any other way) in PHP is there a way to read the LAST line of a file then work backwards? What I am doing: (as per request) I am appending to a .t开发者_如何学编程xt file lines from an input (don't worry about that) and of course append adds to the END of a file so I need to display the contents of a file with one line being one entry to be displayed
So if the contents of the file are: Hello, World! Foo likes Bar! then it needs to display as 1. Foo likes Bar! 2. Hello, World!As far as I know PHP does not know the gets() function. But..
If you use 'file()' and inverse the array you can work it through from bottom to top. That should do the trick for you!
If it is a really big file, it might be better to use the following...
exec("tail -1 input_file > output_file") ;
$string = file_get_contents("output_file") ;
unlink("output_file") ;
This is based on UNIX/Linux system commands, but I'm sure Windows has a command similar to tail.
精彩评论