Search code examples
phpfgets

By pass the first two lines of csv file


I am trying to read data from a csv file using fgets. However, I would want to by pass the first two top rows.

By default, fgets by passes the first row alone. How can I tweak this to make it by pass the first two top rows?

$myfile = "/home/dibon/AML_Data/test.csv";
$myfile = fopen("$myfile", "r") or die("Unable to open file!");  

fgets($myfile); //First fgets to read over header line.

while($line=fgets($myfile)){
//Explode your line by space delimeter
$words=explode("|",$line);

DB::table('transaction_incremental')->insert(
['ReceiptNumber' => "$words[0]", 'InitiatingMSISDN' => "$words[2]"]
);

}

Solution

  • fgetcsv($handle); // get line 0 and move to next
    fgetcsv($handle); // get line 1 and move to next
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        ....
    }