Showing posts with label Read specific row in CSV to using php. Show all posts
Showing posts with label Read specific row in CSV to using php. Show all posts

Sunday 28 April 2019

Read specific row in CSV to using php

<?php

/* This is example for skip particular line from csv */

/*
forexample you have 500 line in your csv but you want read from 125 line then you have
set  $csv->seek(124);

This example used for skip csv file row for read.
This example also used for create new csv after skip csv

*/

$csv_file = 'test.csv';


// try {
//     $csv  = new SplFileObject($csv_file, 'r');
//     $csv->seek(25);
// } catch (RuntimeException $e ) {
//     printf("Error openning csv: %s\n", $e->getMessage());
// }

$fileName ="new_csv.csv";

$dfp = fopen($fileName,'wb');
chmod($fileName,0777);

$csv  = new SplFileObject($csv_file, 'r');
$csv->seek(20);
$lineSkip= 0;
while(!$csv->eof() && ($col = $csv->fgetcsv()) && $col[0] !== null) {
    // $row is a numelrical keyed array  with
    // a string per each field (zero based).
    echo "<pre>";
    print_r($col);
    $lineSkip++;
    fputcsv($dfp,$col);
}
echo $lineSkip;
fclose($dfp);
?>