I am trying to do this exact function, only want to be able to display the last 20 lines of the document?
$file = fopen("/tmp/$importedFile.csv","r");
while ($line = fgetcsv($file))
{
$i++;
$body_data['csv_preview'][] = $line;
if ($i > 20) break;
}
fclose($file);
I have tried changing the "r" in $file = fopen("/tmp/$importedFile.csv","r");however it seems there is only variations of where to put the pointer with read and write.
I feel this could be an easy one. my apologies.
One way to do this is to use an
SqlFileObject. First, you need to know how many lines are in the file, which you can calculate like this:Now you know there are
$linesnumber of lines in the file. Then, you have to seek to the$lines - 20line number, and read your CSV data until EOF, like this:Perhaps there is a more efficient way to calculate
$lines. Also, you should confirm that there is more than 20 lines in the file before attempting toseek()to$lines - 20.