How read ONLY last row ASCII file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
PHP code .....

//Open file.
//read file so long when it isn't end.
while (!feof($file))
//read and print row + LF.
    print "$row<BR>";
//Close file.
?> read file(ascii) and print it display.

How i can read and print only last row - don't all row?

Re: How read ONLY last row ASCII file

Quoted text here. Click to load it

If you are in a *nix system, you can do:

$lastLine = exec('tail -1 getCodeGetText.php');

Another option, much more memory intensive (for large files), but
works on any system:

$lines = file('/path/to/filename.txt');
$lastLine = $lines[(count($lines) - 1)];

Or, you could loop the lines like you are doing already and just not
print out the lines;
the last line will be the last iteration through the loop.

while (!feof($file))
   $line = fgets($fp,1024);

print $line;


Re: How read ONLY last row ASCII file

Quoted text here. Click to load it

WTF? you asked the same stupid question already in sfnet.atk.ohjelmointi  
some time ago and you got 3 replies explaing how. How many times must this  
be explained to you?

1) a single line, the unoptimized easy way:
echo array_pop(file(("file.txt"));

2) modifying your code:
while (!feof($file)){
print "$row<BR>";

3) the real hard-core *nix way:
passthru("tail -n 1 file.txt");
// This one really reads just the last line,
// not all the lines before it. KEWL!

4) using file_get_contents and explode just for fun:
$temp = explode("\r\n",file_get_contents("file.txt"));
echo $temp[sizeof($temp)];

"Ohjelmoija  on  organismi  joka  muuttaa  kofeiinia  koodiksi" - lpk - Satunnaisesti pivittyv nettisarjis | rot13(xvzzb@bhgbyrzcv.arg)  

Re: How read ONLY last row ASCII file

Ur guys,

Have we read ?

If you read the comments, there are some nice functions there

This one looks good:
function readLastLine ($file) {
   $fp = @fopen($file, "r");

   $pos = -1;
   $t = " ";
   while ($t != "\n") {
       if (!fseek($fp, $pos, SEEK_END)) {
           $t = fgetc($fp);
           $pos = $pos - 1;
       } else {
   $t = fgets($fp);
   return $t;

Site Timeline