Jump to content

Read from the LAST line of a CSV file (without knowing line number)


schapel

Recommended Posts

Is there a way to read from the very last line of a CSV file that has been opened via PHP fopen() ?  The trick is that this is a file that has been uploaded by a user, and opened by the server, but could have 100 lines or it could have 100,000.  We just need to read from the very last line specifically of the file, which is basically a line containing an array of variables imprinted on the file during upload.

 

Any thoughts on how to accomplish this?

 

Thanks!

Link to comment
Share on other sites

Got it, but let me throw out a solution and see if it's possible? What if the script already had a total line count from the CSV file (which it determines during a normal import). Is it possible to use some function to utilize this 'total linecount' number to read from that specific line only (which would be the last line of the file).

Link to comment
Share on other sites

I suppose you could do something like this. It will search backwards for a newline. It isn't tested.

 

<?php
$bufferSize = 10;
$file = 'file.txt';

$size = filesize($file);

$fp = fopen($file, 'a');

$buffer = '';
for ($pos = $size - $bufferSize;; $pos -= $bufferSize) {
    $pos = max(0, $pos);
    fseek($fp, $pos2);
    
    $b = fread($fp, $bufferSize);
    $nlPos = strrpos($b, "\n");
    
    if ($nlPos !== false) {
        $buffer = substr($b, $nlPos) . $buffer;
        break;
    }
    else {
        $buffer = $b . $buffer;
    }
    
    if ($pos == 0) {
        break;
    }
}

$lastLine = trim($buffer);

 

Is it possible to use some function to utilize this 'total linecount' number to read from that specific line only (which would be the last line of the file).

 

You don't know how many lines there are before you've read the entire string.

Link to comment
Share on other sites

Okay, I just tested. This will work:

<?php
$bufferSize = 10;
$file = 'test.txt';

$size = filesize($file);

$fp = fopen($file, 'r');
$buffer = '';
for ($pos = $size - $bufferSize;; $pos -= $bufferSize) {
    $pos = max(0, $pos);
    fseek($fp, $pos);
    
    $b = rtrim(fread($fp, $bufferSize));
    $nlPos = strrpos($b, "\n");
    
    if ($nlPos !== false) {
        $buffer = substr($b, $nlPos) . $buffer;
        break;
    }
    else {
        $buffer = $b . $buffer;
    }
    
    if ($pos == 0) {
        break;
    }
}

$lastLine = trim($buffer);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.