Reading CSV end-to-end in PHP

I use PHP to display car GPS device data from a CSV file. This data is captured at least every 30 seconds for more than 70 vehicles and includes 19 data columns. This creates several thousand rows of data and files about 614 KB in size. New data is added to the end of the file. I need to pull out the last row of data for each car, which should be the most recent status. I can pull one line for each block, however, since the CSV file is in chronological order, I pull out the oldest data in the file, not the latest. Is it possible to read CSV from end to start? I saw some solutions, however, they usually include loading the entire file into memory and then reversing it, it sounds very inefficient. Do I have any other options? Thank you for any advice.which you can offer.

EDIT . I use this data to display real-time locations on the fly. The data is provided only to me in CSV format, so I think that import into the database is out of the question.

+3
source share
2 answers

With fseek, you can set the pointer to the end of the file and offset it negatively to read the file back.

+4
source

If you should use csv files instead of a database, perhaps you can read the file one at a time. This will prevent the last line from being stored in memory (thanks to the garbage collector).

$handle = @fopen("/path/to/yourfile.csv", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // old values of $last are garbage collected after re-assignment
        $last = $line;
        // you can perform optional computations on past data here if desired
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);

    // $last will now contain the last line of the file.
    // You may now do whatever with it
}

edit: I did not see the fseek () message. If you need the last line, then this is the way to go.

0
source

All Articles